Android How to get started without creating a new one?

I tried to set the android:launchMode="singleTask" to call the Activity, but it still does not work as I expected.

I need this onCreate(Bundle bundle) method to call only once, but it still calls every time I start Activity.

I start Activity using the following code:

 public void onClick(View v) { Intent myIntent = new Intent(v.getContext(), NextActivity.class); startActivity(myIntent); } 

Please let me know what I'm doing wrong

+2
java android
source share
2 answers

It should be like this:

 android:launchMode="singleTop" 

and call:

 Intent intent= new Intent(context, YourActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

There was already a topic: Android: new Intent () launches a new instance using android: launchMode = "singleTop"

+1
source share

Even if you make your "singleTop" launch mode, a new action will begin.

Each time an activity is created, its onCreate() will be launched.

I understand that the singleTop option basically shuts down the caller.

I think you might think that onCreate() is a form of the application constructor, but it really is an Activity constructor. You might want to do your one-time initializations elsewhere.

0
source share

All Articles