Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 703
ayelder Avatar asked Dec 07 '11 05:12

ayelder


People also ask

What is the difference between created activity object and new activity?

The created activity object, if it had active listeners, or null if it has no event listeners. Creates a new activity if there are active listeners for it, using the specified name, activity kind, parent Id, tags, optional activity links and optional start time. public System.Diagnostics.Activity?

How do I start an activity in Android?

Android Activity startActivity (Intent intent, @Nullable Bundle options) Launch a new activity. Launch a new activity. You will not receive any information about when the activity exits.

How do you start an activity from an intent?

The Intent constructor takes two parameters, a Context and a Class. The Context parameter is used first because the Activity class is a subclass of Context. The Class parameter of the app component, to which the system delivers the Intent, is, in this case, the activity to start.

When to add more properties to the activity object after creation?

There is a scenario that needs to add some more properties to the Activity object after it is created and before it gets started. The reason is the caller wants to avoid creating the objects need to set to the Activity before ensuring the Activity object is created.


2 Answers

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.

like image 94
Urban Avatar answered Nov 15 '22 06:11

Urban


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

like image 30
Pete Houston Avatar answered Nov 15 '22 07:11

Pete Houston