EzDevInfo.com

android-intent interview questions

Top android-intent frequently asked interview questions

How do I pass data between activities on Android?

I have a scenario where, after logging in through a login page, there will be a sign out button on each activity.

On clicking signout, I will be passing the session id of the signed in user to signout. Can anyone guide me on how to keep session id available to all activities??

Alternatively, is there another solution to this problem?


Source: (StackOverflow)

Launch custom android application from android browser

Can anybody please guide me regarding how to launch my android application from the android browser?


Source: (StackOverflow)

Advertisements

What is Intent in Android?

  • What is Intent in Android?
  • Can someone elaborate with an example?
  • What are the types of it, and why we are using it in Android?
  • Why is it so important in Android?

Source: (StackOverflow)

Sending data back to the Main Activity in android

I have two activities. Main activity and child activity. When I press a button, the child activity is launched Now I have no problem. I want to send some data back to the main screen. I used The Bundle class but it is not working. It throw some run time exception Is there any solution? Anyone knows the answer please tell me.


Source: (StackOverflow)

How to get a list of installed android applications and pick one to run

I asked a similar question to this earlier this week but I'm still not understanding how to get a list of all installed applications and then pick one to run.

I've tried:

Intent intent = new Intent(ACTION_MAIN);
intent.addCategory(CATEGORY_LAUNCHER);

and this only shows application that are preinstalled or can run the ACTION_MAIN Intent type.

I also know I can use PackageManager to get all the installed applications, but how do I use this to run a specific application?

Thanks


Source: (StackOverflow)

What is an Android PendingIntent?

I am a newbie to Android. I read the Android Documentation but I still need some more clarification. Can anyone tell me what exactly a PendingIntent is?


Source: (StackOverflow)

How can I open a URL in Android's web browser from my application?

How to open an URL from code in the built-in web browser rather than within my application?

I tried this:

try {
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(download_link));
    startActivity(myIntent);
} catch (ActivityNotFoundException e) {
    Toast.makeText(this, "No application can handle this request."
        + " Please install a webbrowser",  Toast.LENGTH_LONG).show();
    e.printStackTrace();
}

but I got an Exception:

No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com

Source: (StackOverflow)

How to filter specific apps for ACTION_SEND intent (and set a different text for each app)

How can you filter out specific apps when using the ACTION_SEND intent? This question has been asked in various ways, but I haven't been able to gather a solution based on the answers given. Hopefully someone can help. I would like to provide the ability to share within an app. Following Android Dev Alexander Lucas' advice, I'd prefer to do it using intents and not using the Facebook/Twitter APIs.

Sharing using ACTION_SEND intent

Sharing using the ACTION_SEND intent is great, but the problem is (1) I don't want every sharing option there, I'd rather limit it to FB, Twitter, and Email, and (2) I don't want to share the same thing to each sharing app. For example, in my twitter share I'm going to include some mentions and hashtags limited it to 140 chars or less, while the facebook share is going to include a link and a feature image.

Is it possible to limit the options for ACTION_SEND (share) intent? I've seen something about using PackageManager and queryIntentActivities, but haven't been able to figure out the connection between the PackageManager and the ACTION_SEND intent.

OR

Rather than filter the sharing apps, my problem could also be solved if I could use the ACTION_SEND intent to go directly to facebook or twitter rather than popping up the dialog. If that were the case then I could create my own dialog and when they click "Facebook" create a Facebook-specific intent and just send them all the way to Facebook. Same with Twitter.

OR is it not possible? Are the Facebook and Twitter APIs the only way?


Source: (StackOverflow)

Allow user to select camera or gallery for image

What I'm trying to do seems very simple, but after a few days of searching I can't quite figure it out.

I have an application that allows the user to select multiple(up to 5) images. I'm using an ImageView. When the user clicks on the ImageView, I'd like to allow them the option to

  1. Select the image from the gallery, or
  2. Use the camera to capture an image.

I started by using the ACTION_GET_CONTENT intent, and that works well for getting to the gallery. So then I tried using the ACTION_PICK_ACTIVITY intent to allow the user to choose camera or gallery:

Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*"); 
Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
pickIntent.putExtra(Intent.EXTRA_INTENT, camIntent);
pickIntent.putExtra(Intent.EXTRA_INTENT, gallIntent)
pickIntent.putExtra(Intent.EXTRA_TITLE, "Select Source");
startActivityForResult(pickIntent, IMAGE_SELECTOR);

But it appears I can only add one EXTRA_INTENT. The menu show up as expected, but the only options are Gallery and Files....no Camera).

Is there a better/easier way to do this that I'm missing? Thanks for any help.


Source: (StackOverflow)

How to send parameters from a notification-click to an activity?

I can find a way to send parameters to my activity from my notification.

I have a service that creates a notification. When the user clicks on the notification I want to open my main activity with some special parameters. E.g an item id, so my activity can load and present a special item detail view. More specific, I'm downloading a file, and when the file is downloaded I want the notification to have an intent that when clicked it opens my activity in a special mode. I have tried to use putExtra on my intent, but cant seem to extract it, so I think I'm doing it wrong.

Code from my service that creates the Notification:

        // construct the Notification object.
     final Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());


    final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
    contentView.setImageViewResource(R.id.image, R.drawable.icon);
    contentView.setTextViewText(R.id.text, tickerText);
    contentView.setProgressBar(R.id.progress,100,0, false);
    notif.contentView = contentView;        

    Intent notificationIntent = new Intent(context, Main.class);
    notificationIntent.putExtra("item_id", "1001"); // <-- HERE I PUT THE EXTRA VALUE
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notif.contentIntent = contentIntent;

    nm.notify(id, notif);

Code from my Activity that tries to fetch the extra parameter from the notification:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    Bundle extras = getIntent().getExtras();
    if(extras != null){
        Log.i( "dd","Extra:" + extras.getString("item_id") );
    }

The extras is always null and I never gets anything into my log.

Btw... the onCreate is only run when my activity starts, if my activity is already started I also want to collect the extras and present my activity according to the item_id I receive.

Any ideas?


Source: (StackOverflow)

How to use putExtra() and getExtra() for string data

Can someone please tell me how exactly to use getExtra() and putExtra() for Intent. Actually I have a string variable say str, which stores some string data. Now I want to send this data from one activity to the other activity.

  Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
  String keyIdentifer  = null;
  i.putExtra(strName, keyIdentifer );

and then in the SecondScreen.java

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        TextView userName = (TextView)findViewById(R.id.userName);
        Bundle bundle = getIntent().getExtras();

        if(bundle.getString("strName")!= null)
        {
            //TODO here get the string stored in the string variable and do 
            // setText() on userName 
        }

    }

I know it is very basic question but unfortunately I am stuck here. Please help.

Thanks,

edited : Here the string which I am trying to pass from one screen to the other is dynamic. That is I have an editText where I am getting string whatever user types. Then with the help of myEditText.getText().toString() . I am getting the entered value as a string then I have to pass this data.


Source: (StackOverflow)

Android ACTION_IMAGE_CAPTURE Intent

We are trying to use the native camera app to let the user take a new picture. It works just fine if we leave out the EXTRA_OUTPUT extra and returns the small Bitmap image. However, if we putExtra(EXTRA_OUTPUT,...) on the intent before starting it, everything works until you try to hit the "Ok" button in the camera app. The "Ok" button just does nothing. The camera app stays open and nothing locks up. We can cancel out of it, but the file never gets written. What exactly do we have to do to get ACTION_IMAGE_CAPTURE to write the picture taken to a file?

Edit: This is done via the MediaStore.ACTION_IMAGE_CAPTURE intent, just to be clear


Source: (StackOverflow)

Android: Clear the back stack

In Android I have some activities, let's say A, B, C.

In A I use this code to open B:

Intent intent = new Intent(this, B.class);
startActivity(intent);

In B I use this code to open C:

Intent intent = new Intent(this, C.class);
startActivity(intent);

When the user taps a button in C I want to go back to A and clear the back stack (close both B and C). So when the user use the back button B and C will not show up, I've been trying the following:

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

But B and C are still showing up if I use the back button when I'm back in activity A. How can I avoid this?


Source: (StackOverflow)

Using Intent in an Android application to show another activity

In my Android application, I have two activity classes. I have a button on the first one and I want to show the second when it is clicked, but I get an error. Here are the classes:

public class FirstActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button orderButton = (Button)findViewById(R.id.order);

    orderButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        Intent intent = new Intent(FirstActivity.this, OrderScreen.class);
        startActivity(intent);
      }

    });
  }
}

The second class that should show when the button is clicked, but never does:

public class OrderScreen extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.order);

    Button orderButton = (Button) findViewById(R.id.end);

    orderButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        finish();
      }

    });
  }
}

How do I create a button that will show the second activity?


Source: (StackOverflow)

Android Respond To URL in Intent

I want my intent to be launched when the user goes to a certain url: for example, the android market does this with http://market.android.com/ urls. so does youtube. I want mine to do that too.


Source: (StackOverflow)