Recreate activity with different extras - java

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?

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.

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.

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

Related

Where is the difference between onCreate and onStart if both are called upon Activity change anyway? What's the purpose?

I've searched through dozens of Stackoverflow posts and the android doc but just couldn't find the answer.
According to the accepted answer of this SF-post the onCreate method runs when the activity is first created. It also notes that in here views are supposed to be created and list data is being binded.
Then the onStart Method runs but here's the issue. Where's the difference? If you do everything inside of onCreate, switch activities, your app will still display the same data, regardless whether you put the app in the background or switched activities.
So if you declare views in onCreate, what do you do in onStart? initiliaze the views to their R.id.view ? Fetch data?
onResume I suppose is then used for listeners since it's the gas and brake according to this SF-posts accepted answer.
onCreate() is called when the activity is first created. onStart() is called whenever the activity becomes visible, which includes when it is first created (after onCreate()) and after it is coming back to the screen from being stopped (e.g., another activity took over the screen).
So:
Put code in onCreate() that needs to happen when the activity is created (and use onDestroy() to clean it up)
Put code in onStart() that needs to happen either when the activity is created or when the activity returns to the foreground (and use onStop() to clean it up)
Frequently, we do not do anything special when the activity returns to the foreground, in which case you do not need to worry about onStart() or onStop().

Is there a way to tell android force stop activity which going to background without removing it from activity stack?

I have some floating bugs in my app, which unable to reproduce clearly. I suspect them from inproper work of my SaveInstanceState|restoreInstanceState mechanism, so I need to check case, when activity is being stopped when goes to background, and recreating after I press back button from spawned activity. Is there a way to force android stop and destroy activity which went to background? It should remain on activity stack, so I cannot just finish it.
Just enable the developer option "don't keep activities" (or whatever its called). This won't remove the activity from the stack, but will actually call onStop() and onDestroy() whenever the user leaves the activity and opens another. When the user presses BACK, Android will create a new instance of the activity, and call onCreate() and onRestoreInstanceState()` as expected.
Looks like it's a copy of this and this questions. Override onPause() method and call method finish() in it.

OnStart () is called after OnStop() and that is making problems for my aplication

First I would like to say that I am sorry for the vague title. I didn't know what else to name this question.
I have 2 activities (ActivityA, ActivityB). In ActivityA I have overridden the onStop() function and I change value X in a singleton. When I go to ActivityB and onStart() is called I check the value X. The problem is that onStart() is called before onStop() and the value I check in the onStart() function is still the old value before onStop() changes it.
I know why onStart() is called before onStop(). What I am asking you guys is an alternative solution to this problem I am currently having.
I need to save a value in ActivityA before I close it and I need to check for the same value in ActivityB. ActivityB can be accessed from several other activities not just ActivityA. And the otehr activities dont change the value.
SOLVED
I changed onStop() to onPause() That worked. Thanks guys!
don't use singletons.
Activities have the Intent to be used as a communication channel between them.
do like this, in activity A:
Intent i = new Intent(this, ActivityB.class);
i.putExtra("value", 10);
startActivity(i);
then in activity B, you do (at any point you want):
int value = getIntent.getIntExtra("value", 0);
also works for double, string, float, bundle, arrays, etc, as seen in the docs!
Its entirely upto the system to call these methods, better would be to use onPause(); and onResume();
See what the Official Docs say here
The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

How do I close a previously opened layout?

Im needing to know how to destroy previous class, when I open a new class so I dont have a ton of open classes to have to hit the 'back' button through to get back to 1st main class.
I know the layout code within the onCreate is typically this:
setContentView(R.layout.page);
i was thinking like super.destroy();
So when I use an intent to launch another class, I want the inital class to not leave behind open window/ContentView
By class, you mean another activity?
You can call finish(); after you have started the intent;
Intent intent = new Intent(activity1.this, activity2.class);
startActivity(intent);
finish();
Android also takes care of these things himself. If memory is needed, the Dalvik virtual machine will close low priority activities that are running.
onDestroy is called automatically when the Activity is shut down. To remove your previos layout you can just call the method finish() or System.exit(0);

Prevent an Activity from being killed by the OS while starting a child activity

I have a main activity which calls a child one via
Intent I = new Intent(this, Child.class);
startActivityForResult(I, 0);
But as soon as Child becomes visible the main activity gets its onStop and immediately after that onDestroy method triggered. And as soon as I call finish() within the Child activity or press the back button, the Child activity closes and the home screen shows (instead of the main activity).
How can I prevent the main activity from being destroyed? :\
If you launch a child Activity from which you expect return data, you'll probably want to use startActivityforResult instead.
You may want to check this question: Child Activity in Android as it seems to be the same problem.
Edit:
As for what's happening here, you could place code in the onStop() and/or onDestroy() methods to investigate - at least a call to isFinishing() to check why the Activity is being destroyed.
You should also use adb logcat from your host machine to check the logcat in case it holds more information - and maybe use Log.d() (the result goes into logcat as well) instead of toasts to make sure you don't miss them.
I used Dialog instead of an Activity and everything worked well so I'm leaving it like that.
check androidmanifest nohistory=true and that made the OS destroy the activity before the result. that might be one of the reason for your problem.

Categories