Sam YC
Sam YC

Reputation: 11617

How to start Activity arbitrarily

Every time when an Activity is created, I will keep its reference. After that, for some reason, I will want to show it, how can I do that? I don't want to change this Activity to single task or single instance mode, because I want to use it as standard mode.

Below I show some code to clarify the question.

I have this global variable (global across all the Activity) :

ArrayList<Activity> list = new ArrayList<Activity>();

In the Activity onCreate, I will execute the code as below:

public void onCreate(Bundle bundle){
    list.add(this);
}

Now, if I want to show/start the second Activity in the list. How can I do that?

Upvotes: 0

Views: 58

Answers (1)

M-Wajeeh
M-Wajeeh

Reputation: 17284

Don't store Activity's references in a List, instead store their class like this:

list.add(this.getClass());

And later just launch the Activity with FLAG_ACTIVITY_REORDER_TO_FRONT.

Intent intent = new Intent(this, list.get(0));
intent.setFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

Upvotes: 1

Related Questions