[英]Going back to previous activity using ActionBar navigation
In my app, I model Lists of items. 在我的应用程序中,我为项目列表建模。 In MainActivity, I see ListView containing Lists.
在MainActivity中,我看到包含列表的ListView。 I can click on each list to see its items (DisplayListActivity).
我可以单击每个列表以查看其项目(DisplayListActivity)。 On DisplayListActivity, I have button in the ActionBar to display the list properties.
在DisplayListActivity上,我在ActionBar中有一个按钮来显示列表属性。 This launches the third activity (EditListPropertiesActivity).
这将启动第三个活动(EditListPropertiesActivity)。
Main
| ^
V |
DisplayListActivity (listId is passed with Intent, default=1)
| ^
V |
EditListPropertiesActivity (listId is passed with Intent, default=1)
The problem appears, when I select List id=2 on the MainActivity, and then I select the properties button on the DisplayListActivity. 当我在MainActivity上选择List id = 2,然后在DisplayListActivity上选择属性按钮时,会出现问题。 Once I am done with the EditListPropertiesActivity, i click '<' (back) on the ActionBar:
完成EditListPropertiesActivity后,单击ActionBar上的“<”(后退):
.
。
I return to the DisplayListActivity, but instead of going back to the list id=2, I see the list with id=1 (which is default). 我返回到DisplayListActivity,但不是回到列表id = 2,而是看到id = 1的列表(这是默认值)。
How to pass the ListId back form EditListPropertiesActivity to the DisplayListActivity? 如何将ListId从EditListPropertiesActivity传递回DisplayListActivity?
What is the usual solution for that problem? 这个问题通常的解决方案是什么?
Code snippets below. 代码片段如下。
MainActivity 主要活动
public class MainActivity extends Activity {
...
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MetaList mlist = (MetaList) listView.getItemAtPosition(i);
final Intent intent;
intent = new Intent(getApplicationContext(), DisplayListActivity.class);
intent.putExtra(META_LIST_SELECTED, mlist.getId());
startActivity(intent);
}
}
);
...
DisplayListActivity DisplayListActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list);
intent = getIntent();
metaListId = intent.getLongExtra(MainActivity.META_LIST_SELECTED, 1); //read the data or take 1 as default
...
//start the third activity
final Intent intent;
intent = new Intent(getApplicationContext(), EditListPropertiesActivity.class);
intent.putExtra(META_LIST_SELECTED, metaListId);
startActivity(intent);
....
EditListPropertiesActivity EditListPropertiesActivity
public class EditListPropertiesActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_list_parameters);
getActionBar().setDisplayHomeAsUpEnabled(true); //this enables the '<' (back) button
intent = getIntent();
metaListId = intent.getLongExtra(DisplayMetaListActivity.META_LIST_SELECTED, 1);
...
Manifest 表现
<application>
<activity
android:name=".MainActivity">
</activity>
<activity
android:name="com.example.tutorial1.DisplayListActivity"
android:parentActivityName="com.example.tutorial1.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.MainActivity" />
</activity>
<activity
android:name="com.example.tutorial1.EditListPropertiesActivity"
android:parentActivityName="com.example.tutorial1.DisplayListActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.DisplayListActivity" />
</activity>
</application>
try this to finish an activity 试试这个来完成一项活动
Intent intent = new Intent();
intent.putExtra(EXTRA, value);
setResult(RESULT_OK, output);
finish();
and this for getting the result in the previous activity 这是为了在前一个活动中获得结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey(EXTRA)){
// do stuff with data from finished activity
String bla = data.getStringExtra(EXTRA)
}
}
EDIT: read comments! 编辑:阅读评论! need to use startActivityForResult
需要使用startActivityForResult
try to call finish(); 尝试调用finish(); that you should call on destroy of the current activity and display the previous activity.
您应该调用当前活动的销毁并显示以前的活动。
I have found the solution that works. 我找到了有效的解决方案。 I post it here, because it is cleaner (in my opinion) and different than the other proposed.
我在这里发布,因为它更清晰(在我看来)并且与其他提议的不同。 I set the launch mode of the second activity to singleTask in the manifest file:
android:launchMode="singleTask"
我在清单文件中将第二个活动的启动模式设置为singleTask:
android:launchMode="singleTask"
...
<activity
android:name="com.example.tutorial1.DisplayListActivity"
android:label="Meta List"
android:parentActivityName="com.example.tutorial1.MainActivity"
android:launchMode="singleTask">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.MainActivity" />
</activity>
...
If there is a better solution for the stated problem, I am open for discussion :) 如果对于所述问题有更好的解决方案,我愿意讨论:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.