CosmoVitoriano CosmoVitoriano - 6 months ago 38
Java Question

Instagram webpage auto opens when navigated to abous us menu

my app is running into this problem of auto opening a webpage to instagram when I navigate to the about us menu. I only want this to happen once the user has clicked on the instagram text so it presents the option of opening instagram with the insta app or in a webpage
any idea on how to fix this problem?

aboutactivity that has the intents to send the user to corresponding apps

package com.example.vitoriano_vaz.eastbayvapes;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class AboutActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);

Uri number = Uri.parse("tel:9259519029");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);

Uri webpage = Uri.parse("http://www.facebook.com/eastbay");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);

// intent to send user to instagram
Uri uri = Uri.parse("http://instagram.com/_u/eastbay");
final Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);

likeIng.setPackage("com.instagram.android");

try {
startActivity(likeIng);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://instagram.com/eastbay")));
}


TextView instaText = (TextView)findViewById(R.id.textView8);
instaText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
startActivity(likeIng);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://instagram.com/eastbay")));
}
}
});


//Intent to send user to email app

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"eastbay@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(intent, ""));

}
}

Answer

you need to make the activity launch inside an onclick listener not inside you activity main. you will need to do the same thing for your mail intent which is also in your oncreate.

package com.example.vitoriano_vaz.eastbayvapes;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class AboutActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);


        Uri number = Uri.parse("tel:9259619029");
        Intent callIntent = new Intent(Intent.ACTION_DIAL, number);

        Uri webpage = Uri.parse("http://www.facebook.com/eastbayvapes");
        Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);

        // intent to send user to instagram

        Uri uri = Uri.parse("http://instagram.com/_u/eastbayvapes");
        Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);

        likeIng.setPackage("com.instagram.android");

        TextView instaText = (TextView)findViewById(R.id.YOUR_TEXTVIEW_ID);
        instaText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                startActivity(likeIng);
            } catch (ActivityNotFoundException e) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://instagram.com/eastbayvapes")));
            }
        }
    });


        //Intent to send user to email app
      TextView emailText = (TextView)findViewById(R.id.YOUR_TEXTVIEW_ID);
        emailText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("plain/text");
            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"eastbayvapes@gmail.com"});
            intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
            intent.putExtra(Intent.EXTRA_TEXT, "");
            startActivity(Intent.createChooser(intent, ""));
        }
    });


    }
}
Comments