3

I tried to set attribute android:launchMode="singleTask" for calling Activity, but it still does not works as I expected.

I need that method onCreate(Bundle bundle) to be called only once, but it still calls each time when I start Activity.

I start Activity using code like this:

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

Please let me know what I am doing wrong

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
user971511
  • 167
  • 1
  • 2
  • 7

2 Answers2

1

It must be like this:

android:launchMode="singleTop"

and calling:

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

Already existed topic about this: Android: new Intent() starts new instance with android:launchMode="singleTop"

Community
  • 1
  • 1
Pete Houston
  • 14,931
  • 6
  • 47
  • 60
0

Even if you make your launch mode 'singleTop', a new activity will be started.

At each activity creation, its onCreate() will be started.

My understanding is that the singleTop option, basically finishes the caller Activity.

I think that you may think that onCreate() is a form of Application constructor but it really is an Activity constructor. You may want to do your one time initializations elsewhere.

Laurent'
  • 2,611
  • 1
  • 14
  • 22