按下后退按钮和方向更改时,onRestart而不是onCreate

时间:2014-11-19 19:28:47

标签: android android-fragments back-button activity-lifecycle orientation-changes

我有两个活动,A和B(A是他们的父),并且在他们的XML源上都定义了静态片段。

找到问题的步骤:

  1. 活动A开始活动B.
  2. 在活动B上,我按后退按钮返回活动A.
  3. 我旋转我的设备,然后方向发生变化,但现在调用onRestart而不是onCreate,并且(更奇怪的是)活动A中的片段是在活动A上创建的。
  4. 恢复。在后退按钮和方向改变之后,使用A动作栏(???)向我显示B内容视图。 注意:

    • 如果按主页按钮而不是后退按钮,活动也会正常工作。
    • 发生这种情况时,会显示活动A的操作栏(应该如此)。
    • 启动活动B之前的屏幕旋转工作,然后按回按钮。

    我没有使用singleTop 或其他maninfest属性进行该活动。

    我有什么问题? (抱歉我的英语不好)。

    活动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);
    }
    
    
    }
    

    活动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;
    }                
    
    }
    

    我的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 个答案:

答案 0 :(得分:0)

我找到了问题的根源!

我用活动A的意图启动了活动B.

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

而不是那个,现在我用它:

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