Arik6
Arik6

Reputation: 123

Recreate activity with different extras

I need to recreate Activity - open a new screen with different extras, when I try to do that with recreate() it works perfectly, but there is an ugly black screen.

 setIntent(intent);
 this.recreate();

When I try to finish() and startActivity(newIntent) there is no black screen but in some way onCreate() called before onDestroy()

 finish();
 startActivity(newIntent);

this is my newIntent:

 Intent newIntent = new Intent(this, DeviceActivity.class);
 newIntent.putExtra(Consts.INTENT_ID, device.getID());
 newIntent.putExtra(Consts.INTENT_DEVICE_NAME, device.getID());

Any ideas?

Upvotes: 0

Views: 362

Answers (3)

Arik6
Arik6

Reputation: 123

Ok, so the problem was that I had connecting/disconnecting to some manager that fired from onCreate() & onDestroyed().

onDestroyed() Happens asynchronously and we have no way of knowing when the system will reach it (Even we call finish()).

so after I moved this manager launching to onPause() & onResume() it works smoothly and it solve my problem.

thank you all for your help

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83527

One solution here is to call startActivity() without calling finish(). This will create a new instance of your activity, basically a new screen but using the same code.

But this depends entirely on what you want the user to see. We should talk about this in terms of the user interaction, not in terms of "activities" and "intents" which are implementation details.

Upvotes: 0

Martin Zeitler
Martin Zeitler

Reputation: 76649

It does not matter how often onCreate() is being called; just check for savedInstanceState == null, in order to determine when to run code within that method. When it runs for the first time, savedInstanceState will always be null. Moving some heavy code outside of method onCreate() might also make sense, in case it could run later on in the Activity life-cycle.

Upvotes: 1

Related Questions