Skip to content Skip to sidebar Skip to footer

Android Activity Restart(due To Screen Orientation Changes) Issue Solutions Provided On Stackoverflow Dont Work For Me

I am making a demo app to illustrate how I handle screen orientation. It is a simple counter app that increments the count when the user presses the increment button and viceversa.

Solution 1:

Why you are Overriding onConfigurationChanged while you don't have need to do this...in your case.don't remove android:configChanges="keyboardHidden|orientation" in manifest file just remove the code

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
//UIActivity();
}

from your project.Clean it and rebuild ...

Solution 2:

You don't say what didn't work, but I'll venture a guess. You're getting a null pointer exception somewhere. One problem with your code is that you'll need to repeat most of the logic that you have in onCreate() when you execute onConfigurationChanged. You have an entirely new set of views in the activity. Otherwise, the member field tv will be null and there won't be listeners attached to your buttons.

EDIT From your comment that count isn't being preserved, I believe that there's something wrong with your manifest and the system is destroying and re-creating your activity on orientation changes. To preserve count across such changes, override onRetainNonConfigurationInstance() to return an Integer containing the value of count and access that value in onCreate() by calling getLastNonConfigurationInstance() and casting it to an Integer.

Solution 3:

Remove this method

@OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
      ....
}

and Make sure in manifest you are adding

android:configChanges="keyboardHidden|orientation"

With Counter_demoActivity or some where else?

Just Use below line and check if your activity is recreating or not? Log a line in your activity on create method and then change orientation. Is line appear in logcat or not?

android:configChanges="orientation|keyboard|keyboardHidden"

Solution 4:

Here is a trick I found in a blog when I had the same issue. Try it

Create a Method getsavedstuff()

privatevoidgetsavedstuff() {
    Object mObject  = getLastNonConfigurationInstance();
}

and use it right after setContentView

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getsavedstuff();
}

Solution 5:

Now here is a solution that makes sense:)

publicclassCounter_demoActivityextendsActivity {
final privatestaticStringCOUNT_TITLE = "title";
private int restoredCount;
int count;
TextView tv;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button add1=(Button)findViewById(R.id.btn1);
    Button sub1=(Button)findViewById(R.id.btn2);
    tv=(TextView)findViewById(R.id.tv1);
    add1.setOnClickListener(newView.OnClickListener() {
    publicvoidonClick(View v) {
        count++;
        tv.setText("Your Count is " + count);
    }
});
   sub1.setOnClickListener(newView.OnClickListener() {
    publicvoidonClick(View v) {
        count--;
        tv.setText("Your Count is " +count);
    }
});
}
@OverrideprotectedvoidonSaveInstanceState(Bundle outState) {
    outState.putInt(COUNT_TITLE, count);
    super.onSaveInstanceState(outState);
}

@OverrideprotectedvoidonRestoreInstanceState(Bundle savedInstanceState) {
     restoredCount =  savedInstanceState.getInt(COUNT_TITLE);
     count=restoredCount;
                 tv.setText("Your Count is " + restoredCount);
    super.onRestoreInstanceState(savedInstanceState);
}

}

Post a Comment for "Android Activity Restart(due To Screen Orientation Changes) Issue Solutions Provided On Stackoverflow Dont Work For Me"