views:

51

answers:

1

I was wondering is it possible to create multiple instances of a single Activity in Android?

I currently start my own inCall screen for a Voip Test by using the following code:


     public void initInCallScreen(String pName, String phoneNumber, int contactID, boolean 
        callDirection, int lineID){

    //starts in callScreen dialog
    final Intent myIntent = new Intent(context, CallDialogActivity.class);
    myIntent.putExtra("NAME", pName);
    myIntent.putExtra("NUMBER", phoneNumber);
    myIntent.putExtra("ID", contactID);
    myIntent.putExtra("CALLTYPE", callDirection); //True = Incoming, False = Outgoing
    myIntent.putExtra("LINEID", lineID);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);

This allows me to start the Activity fine.

However when I call it for a second it just returns to the Activity already created rather than creating a new Activity and placing it on the stack.

I would like to be able to create the activity multiple times so that I have two or 3 Activities on the stack and the user can switch between them, using Home, Back buttons etc...

Is this possible and if so what am I doing wrong?

+2  A: 

However when I call it for a second it just returns to the Activity already created rather than creating a new Activity and placing it on the stack.

You probably changed your manifest to add an android:launchMode attribute that is interfering with your goal. By default, starting an activity starts a new instance.

Also:

  • Get rid of myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);, since you do not want a new task based on what you have written here
  • Since context is probably a Context, I do not know why you are going through all of the ContextWrapper / getBaseContext() stuff
CommonsWare
Sorry, moving old code into a new project, thanks for pointing the context out. When I remove myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); I get the following error when trying to start the Activity :AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?So I'm not sure but I think I need the FLAG_ACTIVITY_NEW_TASK flag set? Also, there is no launchMode setting anywhere in my manifest.
Donal Rafferty
@Donal Rafferty: Where are you launching this from?
CommonsWare
From a Service within the same Application.
Donal Rafferty
@Donal Rafferty: Hmmm...I have never started an activity from a service, so I don't know how the rules differ. Sorry!
CommonsWare