我遇到back navigation
的问题,因为它没有恢复最后一个片段。所有活动均为AppCompatActivity
,使用导航工具(ActivityA
)和scroll Studio活动(ActivityB
)Android Studio
ActivityA
显示fragment_1
(确定)fragment_1
替换为fragment_2
(确定)ActivityB
中的按钮启动fragment_2
(确定)
ActivityB
(好)back
的{{1}}导航,则会显示ActivityB
而不是fragment_1
(不是很好) 我所尝试的是:
fragment_2
再次渲染,但它不起作用。有任何帮助吗?感谢
答案 0 :(得分:1)
嗨Nguyen我创建了相同的场景,但在从activityB
返回后将fragment_2放在顶部这是我的代码 `public class FirstActivity扩展ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment1()).commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment1 extends Fragment {
public PlaceholderFragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
Button button = (Button) rootView.findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment2()).commit();
}
});
return rootView;
}
}
public static class PlaceholderFragment2 extends Fragment {
public PlaceholderFragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second, container, false);
Button button = (Button) rootView.findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Secondctivity.class);
startActivity(intent);
}
});
return rootView;
}
}
} `
这是我的清单文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Secondctivity"
android:label="@string/title_activity_secondctivity" >
</activity>
</application>
答案 1 :(得分:1)
我忘了一个简单的解决方案是Activity Launch Mode。我像这样修改了Manifest文件中的活动,一切正常。
<activity
android:name=".ActivityA"
android:launchMode="singleTask"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
问题是back navigation
按钮试图再次创建ActivityA
(可能是因为滚动活动模板行为)。