2

How to prevent to create every time Instance of Store activity?

When I call startActivity(new Intent(this,StoreActivity.class)), it will create new instance and call OnCreate Method in StoreActivity. I want to call one time Oncreate.

Is this Possible ?

Bajirao Shinde
  • 1,356
  • 1
  • 18
  • 26
Aditya Patel
  • 69
  • 1
  • 8
  • Not possible. You should look at this documentation http://developer.android.com/training/basics/activity-lifecycle/recreating.html and use similar concepts to save the activity state (when your activity is destroyed) and then reuse those saved states when you recreate the activity from an intent. – kha Feb 26 '16 at 13:28

4 Answers4

1

Do this

startActivity(new Intent(
       this,StoreActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));

From Android Documentation

public static final int FLAG_ACTIVITY_SINGLE_TOP

If set, the activity will not be launched if it is already running at the top of the history stack.

Constant Value: 536870912 (0x20000000)

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • What do you mean by its not working? Is it still launching new instance even if activity is open? – Rohit5k2 Feb 26 '16 at 12:29
  • when i call storeActivity from MainActivity , OnCreate Method call during create instance of StoreActivity , Now i pressed back , onBackpressMethod call from Storeactivity ... and i am back on MainActivity .. Now it should not be call Oncreate Method of StoreActivity when i redirect from Main Activity to Store Activity .. Because i have already created instance of StoreActivity .. – Aditya Patel Feb 26 '16 at 12:48
  • That's not how it works. This is against the Android Framework flow. You should re-think about what you are trying to achieve. – Rohit5k2 Feb 26 '16 at 12:50
1

Start your activity like this:

Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(storeIntent);

It will call onCreate() only once on first launch of activity, if activity is already running it calls only onNewIntent() instead of create new instance of activity or calling onCreate.

Deven
  • 3,078
  • 1
  • 32
  • 34
0

This is not possible since each time you press back button, onBackPressed() method is called which actually destroys your StoreActivity.

If you want this method to not destroy your activity, just remove or comment out super.onBackPressed() line in this method.

In this case your activity will not be destroyed when back button is pressed, but then you will have to use any other logic to bring your MainActivity to top of the stack.

Muhammad Ibrahim
  • 289
  • 2
  • 12
0

try to finish the first activity after starting the new one, add this code after the Intent command.

finish();

like that:

startActivity(new Intent(this, newActivity.class));
finish();

check this link Finish an activity from another activity

Samuel
  • 65
  • 8