繁体   English   中英

当按下后退按钮并改变方向时,使用onRestart代替onCreate

[英]onRestart instead of onCreate when press back button and orientation change

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

找到我的问题的步骤:

  1. 活动A开始活动B。
  2. 在活动B上,我按返回按钮,然后返回到活动A。
  3. 我旋转设备,然后改变方向,但是现在调用了onRestart而不是onCreate,(更奇怪的是)在活动A上创建了活动B的片段。

正在恢复。 后退按钮和方向更改后,B内容视图通过A动作栏(???)显示给我。 笔记:

  • 如果我按下主页按钮而不是后退按钮,则活动也将正常进行。
  • 发生这种情况时,将显示活动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>

我找到了问题的根源!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM