簡體   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