OnPause / onRestore with savedInstanceState

I am new to Android development, and I need help in maintaining an activity state. What is the correct way to save an instance from onPause and restore it from onRestore, since, obviously, Android does not send the savedInstanceState packet, as it does with onCreate or onSaveInstanceState, for example. Or is there a better way to save besides using the savedInstanceState package?

It makes sense?

[edit] Well, I think I know what my real problem is ... But first, I think I was looking for using SharedPreferences instead of savedInstanceState.

So, making more debugging logs of browsing, I notice that instead of bringing activity at the top of the stack, it creates a new one. Yes, I understand that I am creating a new one.

Intent itemintent = new Intent(MediaList.this, AudioPlayer.class); Bundle b = new Bundle(); //...putString some strings to send itemintent.putExtra("android.intent.extra.INTENT", b); itemintent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivityForResult(itemintent,0); 

... But shouldn't FLAG_ACTIVITY_REORDER_TO_FRONT prevent the creation of a new action? I assume that he thinks he should create a new one, so how do I send multiple lines?

Even better, how can I check if this activity is already on the stack and switch to it if the lines are the same? - I start this operation when the user clicks a multimedia item from the list. [/ Edit]

+6
source share
2 answers

For some reason, this is not documented by very bold neon blinking letters, and it took me a while to discover, but you don't have to worry about whether your activity exists or not, if you just create it using android: launchMode = [ "multiple" | "singleTop" | "singleTask" | "SingleInstance"] property is set to "singleInstance".

Then there is only one instance, and your memory fields are saved until activity has been garbage collected.

Another thing you can do is create an application (extended from the application) and save all your permanent objects in it ... it is created first and destroyed last until the entire application life cycle (including all your services that are not active).

Just create an application class and specify it in your manifest as follows:

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".MyApplication"> 

Then, if you still REALLY want to keep your values ​​in situations where the application will be closed, just use SharedPreferences.

+3
source

What kind of onRestore method are you talking about? This is not part of the Activity lifecycle ... Suppose you mean onRestart . In any case, the reason you are not getting the onRestart is because you do not need it. Your activity has not been officially "killed", so you do not need to restore from a saved state. Your activity has been suspended but not deleted from memory, so the system simply informs you that it has become visible again. You probably don't need to do anything for this kind of transition event.

In addition, the way to do this is to save everything you consider the state value in onSaveInstanceState() , and then restore them to onCreate . After that, you can restore any view-specific properties either in onCreate itself or later in the activity lifecycle (for example, onResume ).

+1
source

Source: https://habr.com/ru/post/886666/


All Articles