onRestart instead of onCreate when press back button and orientation change

368 views Asked by At

I have two activities, A and B (A is their parent), and both there are static fragments defined on their XML sources.

The steps to find my problem:

  1. Activity A start Activity B.
  2. On Activity B, I press the back button and go back to Activity A.
  3. I rotate my device and then orientations change, but now onRestart is called instead of onCreate and (the more strange thing) the fragments from Activity B are created on Activity A.

Resuming. after backbutton and orientation change, the B content view is shown to me with the A action bar (???). Notes:

  • If i press the home button instead of back button, the activities working as well.
  • When this occurs, the action bar from Activity A is shown (as it should).
  • Screen rotation work as well before start Activity B and press back button.

I'm not using singleTop or other maninfest properties to that activities.

What is wrong with me? (sorry for my bad english).

Activity A:

public class ChoosePaymentMethodActivity extends Activity{

private Checkout checkout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ToFinishReceiver.registerActivity(this);
    setContentView(R.layout.activity_choose_payment_method);

    checkout = getIntent().getParcelableExtra(Checkout.PARCEL_KEY);
}            

//defined on XML
public void onClickOnDelivery(View v){
    nextStep(DeliveryCheckoutActivity.class);
}   

private void nextStep(Class<?> clazz){
    Intent intent = getIntent();
    intent.putExtra(Checkout.PARCEL_KEY, checkout);
    intent.setClass(this, clazz);
    startActivity(intent);
}


}

Activity B:

public class DeliveryCheckoutActivity extends Activity {

protected static final String CHECKOUT_FRAGMENT_TAG = "CONGA_LA_CONGA";
private DeliveryCheckoutFragment fragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setUpActivity();

    checkout = getIntent().getParcelableExtra(Checkout.PARCEL_KEY);
    fragmentManager = getFragmentManager();

    findFinishCheckoutFragment();

    if(savedInstanceState == null) addCheckoutFragment();
    else findCheckoutFragment();

    if(savedInstanceState != null) fragment = (DeliveryCheckoutFragment) getCheckoutFragment();
}

@Override
protected CheckoutFragment createCheckoutFragment() {
    fragment = new DeliveryCheckoutFragment();
    return fragment;
}

protected void addCheckoutFragment(){
    checkoutFragment = createCheckoutFragment();
    fragmentManager.beginTransaction()
                   .replace(android.R.id.content, checkoutFragment, CHECKOUT_FRAGMENT_TAG)
                   .commit();
}

protected CheckoutFragment findCheckoutFragment(){
    checkoutFragment =
            (CheckoutFragment) fragmentManager.findFragmentByTag(CHECKOUT_FRAGMENT_TAG);
    return checkoutFragment;
}                

}

My AndroidManinfest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="br.com.lexsis.crazyapp" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true" />

    <application
        android:name=".CrazyApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".SplashActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Home" >
        </activity>

        <activity
            android:name=".requestfood.request.ChoosePaymentMethodActivity"
            android:label="@string/payment_method"
            android:parentActivityName=".MainActivity"
            android:theme="@style/Theme.Purple" >
        </activity>

        <activity
            android:name=".requestfood.request.DeliveryCheckoutActivity"
            android:label="@string/payment"
            android:parentActivityName=".requestfood.request.ChoosePaymentMethodActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Purple" >
        </activity>

    </application>

</manifest>
1

There are 1 answers

0
pablobaldez On BEST ANSWER

I found the source of the problem!

I was starting Activity B with Intent of Activity A.

private void nextStep(Class<?> clazz){
    Intent intent = getIntent();
    intent.putExtra(Checkout.PARCEL_KEY, checkout);
    intent.setClass(this, clazz);
    startActivity(intent);
}

instead of that, now i use this:

private void nextStep(Class<?> clazz){
    Intent intent = new Intent(this, clazz);
    intent.putExtras(getIntent());
    intent.putExtra(Checkout.PARCEL_KEY, checkout);
    startActivity(intent);
}