我试图保留Fragment
的状态。我有MotherActivity
其中FragmentOne
,FragmentTwo
,FragmentThree
和FragmentTwo
内部我正在调用另一个活动ChildActivity
。问题是当我在ChildActivity
中按下后退按钮时,它会刷新MotherActivity
而不保持FragmentTwo
的状态,而是显示我第一次来的FragmentOne
。
我只希望当我从BackButton
按ChildActivity
时,容器FragmentTwo
应该在那里:
MainAcitivity:
public class MotherActivity extends FragmentActivity {
AHBottomNavigation bottomNavigation;
Fragment selectedFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigation = (AHBottomNavigation) findViewById(R.id.navigation);
bottomNavigation.addItem(item1);
bottomNavigation.addItem(item2);
bottomNavigation.addItem(item3);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
if (position == 0) {
selectedFragment = FragmentOne.newInstance(bottomNavigation);
} else if (position == 1) {
selectedFragment = FragmentTwo.newInstance(bottomNavigation);
} else if (position == 2) {
selectedFragment = FragmentThree.newInstance(bottomNavigation);
}
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout,selectedFragment);
fragmentTransaction.commit();
return true;
}
});
}
@Override
protected void onStart() {
super.onStart();
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, FragmentOne.newInstance(bottomNavigation));
fragmentTransaction.commit();
}
public void setNotification(){
bottomNavigation.setNotification("1", 1);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super().
}
@Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(113, intent);
finish();
return;
}
}
FragmentTwo
我来自childActivity
:
public class FragmentTwo extends Fragment {
public static AHBottomNavigation bottomNavigation1;
Card_detail_Adapter_Footer searchabledapter;
public static boolean showAddress = false;
Fragment selectedFragment;
public static FragmentTwo newInstance(AHBottomNavigation bottomNavigation) {
FragmentTwo fragment = new FragmentTwo();
bottomNavigation1 = bottomNavigation;
showNotificationCounter(3);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart, container, false);
if(showAddress == true){
showAddress = false;
// From here I am calling ChildActivity
Intent intent = new Intent(getActivity(), ChildActivity.class);
startActivity(intent);
}
return view;
}
}
在ChildActivity
中简单地调用onBackPressed()
答案 0 :(得分:1)
问题在于onStart()
的{{1}}方法,因为当您从MotherActivity.java
打开另一项活动时,您的MotherActivity FragmentTwo
会被调用。当您通过提交交易时,从onStop()
调用ChildActivity
的{{1}}后退按钮,该按钮正在从onStart()
替换MotherActivity
。
请查看https://developer.android.com/guide/components/activities/activity-lifecycle.html
答案 1 :(得分:0)
将onStart()
方法的代码放入onCreate()
方法并检查配置是否已更改:
if (savedInstanceState == null) {
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout, FragmentOne.newInstance(bottomNavigation));
fragmentTransaction.commit();
}