【问题标题】:onRestart instead of onCreate when press back button and orientation change当按下返回按钮和方向改变时,onRestart 而不是 onCreate
【发布时间】:2014-11-19 19:28:47
【问题描述】:

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

找到我的问题的步骤:

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

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

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

我没有在该活动中使用 singleTop 或其他清单属性。

我怎么了? (对不起我的英语不好)。

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

【问题讨论】:

  • 给我们你的源代码

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


【解决方案1】:

我找到了问题的根源!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多