Screen processing without data loss - Android

I get crazy by figuring out how best to control screen rotation. I read hundreds of questions / answers here, but I'm really confused.

How to save myClass data before activity is recreated, so I can save everything for redrawing without unnecessary initialization?

Is there a cleaner and better way than just?

I need to handle the rotation because I want to change the layout in landscape mode.

public class MtgoLifecounterActivity extends Activity { MyClass myClass; // Called when the activity is first created @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); If ( ?? first run...myClass == null ? ) { myClass = new MyClass(); } else { // do other stuff but I need myClass istance with all values. } // I want that this is called only first time. // then in case of rotation of screen, i want to restore the other instance of myClass which // is full of data. } 
+29
android android-layout android-intent
Apr 12 2018-12-12T00:
source share
6 answers

can use the override method onSaveInstanceState() and onRestoreInstanceState() . or stop calling onCreate() when you rotate the screen, just add this line to the xml android:configChanges="keyboardHidden|orientation" manifest android:configChanges="keyboardHidden|orientation"

note: your custom class should implement the Parcelable example below.

 @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable("obj", myClass); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onRestoreInstanceState(savedInstanceState); myClass=savedInstanceState.getParcelable("obj")); } public class MyParcelable implements Parcelable { private int mData; public int describeContents() { return 0; } /** save object in parcel */ public void writeToParcel(Parcel out, int flags) { out.writeInt(mData); } public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() { public MyParcelable createFromParcel(Parcel in) { return new MyParcelable(in); } public MyParcelable[] newArray(int size) { return new MyParcelable[size]; } }; /** recreate object from parcel */ private MyParcelable(Parcel in) { mData = in.readInt(); } } 
+21
Apr 12 2018-12-12T00:
source share
— -

In the manifest activity tag, you should mention

 <activity android:name="com.example.ListActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation"> 

If you are using Android 2.3 (API level 13) and above, use

 <activity android:name="com.example.Activity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize"> 

It should work.

It will only work with the activity tag, not with the application tag

+25
Nov 29 '13 at 6:22
source share

Perhaps this has already been decided, but only for a small update for new members who are stuck on it, just look at the Google developer site , from API level 13, you just need to add this code to the manifest:

 <activity android:name=".SplashScreensActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/app_name"> 

when one of these configurations changes, SplashScreensActivity does not restart. Instead, SplashScreensActivity receives a call to onConfigurationChanged (). This method is passed to the Configuration object, which defines the new device configuration. By reading the fields in the Configuration, you can define a new configuration and make the appropriate changes by updating the resources used in your interface. When this method is called, the Activity Activities object is updated to return resources based on the new configuration, so you can easily reset your user interface elements without restarting the system.

+11
Jan 14 '15 at 7:37
source share

There are two (good) ways about this. Add your class to Parcelable and put it in a package in onSaveInstanceState() or, if it is more complex (like AsyncTask), return it to onRetainNonConfigurationInstance() .

Then there is also a lazy way when you simply stop responding to configuration changes.

+2
Apr 12 2018-12-12T00:
source share

If you do not need to restart your activity, just set the configChanges attribute in your activity in AndroidManifest.xml:

  android:configChanges="keyboard|keyboardHidden|orientation" 

This will tell the operating system that you will handle the rotation and not restart your activity. Using this method saves you from having to save any type of state.

+1
Apr 12 2018-12-12T00:
source share

The problem is that you are losing the "state" of the application. In the PLO, what is a state? Variables! Exactly! Therefore, when you lose the data of your variables.

Now here is what you can do, find variables that lose their state.

enter image description here

When you rotate your device, your current activity is completely destroyed, i.e. passes through onSaveInstanceState () onPause() onStop() onDestroy() , and a new action is created completely, which passes through onCreate() onStart() onRestoreInstanceState .

Two methods are shown in bold, onSaveInstanceState () saves an instance of the current activity that will be destroyed. onRestoreInstanceState This method restores the saved state of a previous action. Thus, you will not lose the previous state of the application.

Here is how you use these methods.

  @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); outState.putString("theWord", theWord); // Saving the Variable theWord outState.putStringArrayList("fiveDefns", fiveDefns); // Saving the ArrayList fiveDefns } @Override public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) { super.onRestoreInstanceState(savedInstanceState, persistentState); theWord = savedInstanceState.getString("theWord"); // Restoring theWord fiveDefns = savedInstanceState.getStringArrayList("fiveDefns"); //Restoring fiveDefns } 
0
Jul 25 '17 at 5:07 on
source share



All Articles