[英]onRestart instead of onCreate when press back button and orientation change
我有兩個活動,A和B(A是其父級),並且在XML源上都定義了靜態片段。
找到我的問題的步驟:
正在恢復。 后退按鈕和方向更改后,B內容視圖通過A動作欄(???)顯示給我。 筆記:
我沒有對該活動使用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.