ayelder
ayelder

Reputation: 309

startActivity creating new instance of Activity every time

I am calling startActivity to pass data from one activity to another using an activity's context in an external class.

This is one example of how I create the intent to be sent:

public static Intent createSearchIntent(Context context, Class<?> cls) {
    Intent i = new Intent(ACTION_SEARCH, null, context, cls);
    return i;
}

This is how the I start an activity:

mContext.startActivity(mIntent); 

EDIT: Sorry, I was mistaken in what happens. The activity is not destroyed when I call startActivity, however the activity I am sending the intent to always has it's onCreate method called so I am guessing that a new instance of the activity is being created instead of returning to the paused/stopped one.

How would I be able to change it so that I can just return to the paused/stopped Activity?

Upvotes: 6

Views: 11454

Answers (2)

Urban
Urban

Reputation: 2199

This is when you need to use flags. For making a previously started activity to come back to the top of the stack you need to add the i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); flag to your intent and then start that activity-startActivity(i) with 'i' being the intent name.
For a list of other flags have a look here.

Upvotes: 8

Pete Houston
Pete Houston

Reputation: 15069

Calling Activity B from Activity A doesn't by default does not destroy Activity A itself, what you see is Activity B displayed over Activity A, screen-overlapping. You can check by pressing Back button.

It's the Activity lifecycle: http://developer.android.com/reference/android/app/Activity.html

Upvotes: 4

Related Questions