Fragment java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
MainActivity는 Fragment를 사용하여 밑에 있는 메뉴를 실현한다. 밑에 네 개의 메뉴 단추가 있는데 각각 AFragment, BFragment, CFragment, DFragment에 대응한다.여기서 AFragment는 기본 디스플레이입니다.
CFragment에서 button을 누르면 두 번째Activity 인터페이스:SecondActivity로 이동합니다.
SecondActivity 반환 키는 두 가지가 있습니다:button01,button02.그 중에서 button01은 CFragment를 되돌려줍니다.button02가 되돌아오는 것은Afragment입니다.
문제가 발생했습니다.
button01은MainActivity의CFragment로 되돌아갈 수 있지만button02는MainActivity의AFragment로 되돌아갈 수 없습니다.SecondActivity에서finish()를 사용하든Intent를 사용하든MainActivity의AFragment로 되돌아갈 수 없습니다.
마지막으로 방송 메커니즘을 생각해서 실현했다.
가서 또 다른 문제가 생겼다.
Fragment java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState.
마지막fragmentTransaction.commit Allowing State Loss () 가fragment Transaction을 대체합니다.commit () 문제 해결
마지막으로 코드를 찾아보십시오.
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
public class MainActivity extends BaseActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
private FragmentManager fragmentManager;
private TextView txt_home;
private TextView txt_find;
private TextView txt_collection;
private TextView txt_setting;
private FragmentTransaction transaction;
private HomeFragment homeFragment;
private FindFragment findFragment;
private CollectionFragment collectionFragment;
private SettingFragment settingFragment;
private ImageButton imgbtn_voice;
private final static int SCANNIN_GREQUEST_CODE = 65537;
private BackBroadcast backBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ResourceHelper.activityContext = this;
setContentView(R.layout.activity_main);
initImageLoaderConfi();
initView();
}
/**
*
*/
private void initView() {
fragmentManager = getSupportFragmentManager();
txt_home = (TextView) findViewById(R.id.home_text);
txt_find = (TextView) findViewById(R.id.find_text);
txt_collection = (TextView) findViewById(R.id.collection_text);
txt_setting = (TextView) findViewById(R.id.setting_text);
txt_home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setTabSelection(0);
}
});
txt_find.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setTabSelection(1);
}
});
txt_collection.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setTabSelection(2);
}
});
txt_setting.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setTabSelection(3);
}
});
setTabSelection(0);
}
@SuppressLint("NewApi")
public void setTabSelection(int index) {
clearSelection();
transaction = fragmentManager.beginTransaction();
hideFragments(transaction);
switch (index) {
case 0:
// tab ,
Drawable drawable01 = this.getResources().getDrawable(R.drawable.home_home_pressed);
txt_home.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable01, null, null);
txt_home.setTextColor(Color.parseColor("#2786c8"));
if (homeFragment == null) {
homeFragment = new HomeFragment();
transaction.add(R.id.content, homeFragment);
} else {
transaction.show(homeFragment);
}
break;
case 1:
Drawable drawable02 = this.getResources().getDrawable(R.drawable.home_find_pressed);
txt_find.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable02, null, null);
txt_find.setTextColor(Color.parseColor("#2786c8"));
if (findFragment == null) {
findFragment = new FindFragment();
transaction.add(R.id.content, findFragment);
} else {
transaction.show(findFragment);
}
break;
case 2:
Drawable drawable03 = this.getResources().getDrawable(R.drawable.home_collection_pressed);
txt_collection.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable03, null, null);
txt_collection.setTextColor(Color.parseColor("#2786c8"));
if (collectionFragment == null) {
collectionFragment = new CollectionFragment();
transaction.add(R.id.content, collectionFragment);
} else {
transaction.show(collectionFragment);
}
break;
case 3:
Drawable drawable04 = this.getResources().getDrawable(R.drawable.home_setting_pressed);
txt_setting.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable04, null, null);
txt_setting.setTextColor(Color.parseColor("#2786c8"));
if (settingFragment == null) {
settingFragment = new SettingFragment();
transaction.add(R.id.content, settingFragment);
} else {
transaction.show(settingFragment);
}
break;
}
transaction.commitAllowingStateLoss(); // , transaction.commit() 。
}
//
@SuppressLint("NewApi")
private void clearSelection() {
txt_home.setTextColor(Color.parseColor("#82858b"));
Drawable drawableHome = this.getResources().getDrawable(R.drawable.home_home_normal);
txt_home.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableHome, null, null);
txt_find.setTextColor(Color.parseColor("#82858b"));
Drawable drawableFind = this.getResources().getDrawable(R.drawable.home_find_normal);
txt_find.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableFind, null, null);
txt_collection.setTextColor(Color.parseColor("#82858b"));
Drawable drawableMy = this.getResources().getDrawable(R.drawable.home_collection_normal);
txt_collection.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableMy, null, null);
txt_setting.setTextColor(Color.parseColor("#82858b"));
Drawable drawableMore = this.getResources().getDrawable(R.drawable.home_setting_normal);
txt_setting.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableMore, null, null);
}
// Fragment, Fragment
private void hideFragments(FragmentTransaction transaction) {
if (homeFragment != null) {
transaction.hide(homeFragment);
}
if (findFragment != null) {
transaction.hide(findFragment);
}
if (collectionFragment != null) {
transaction.hide(collectionFragment);
}
if (settingFragment != null) {
transaction.hide(settingFragment);
}
}
@Override
protected void onResume() {
super.onResume();
// setTabSelection(0);
backBroadcast = new BackBroadcast();
registerReceiver(backBroadcast, new IntentFilter(Constant.HOME_BACK_ACTION));
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(backBroadcast);
}
private class BackBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String temp = intent.getAction();
if (temp.equals(Constant.HOME_BACK_ACTION)) {
setTabSelection(0);
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
}
겸사겸사 포석도 붙여주세요.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/home_bottom"
android:baselineAligned="true" >
<TextView
android:id="@+id/home_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:drawableTop="@drawable/home_home"
android:gravity="center"
android:text=" "
android:textColor="#82858b" />
<TextView
android:id="@+id/find_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:drawableTop="@drawable/home_find"
android:gravity="center"
android:text=" "
android:textColor="#82858b" />
<TextView
android:id="@+id/collection_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:drawableTop="@drawable/home_collection"
android:gravity="center"
android:text=" "
android:textColor="#82858b" />
<TextView
android:id="@+id/setting_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:drawableTop="@drawable/home_setting"
android:gravity="center"
android:text=" "
android:textColor="#82858b" />
</LinearLayout>
</LinearLayout>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.