在我的应用中,我为项目列表建模。在MainActivity中,我看到包含列表的ListView。我可以单击每个列表以查看其项目(DisplayListActivity)。在DisplayListActivity上,我在ActionBar中具有按钮以显示列表属性。这将启动第三个 Activity (EditListPropertiesActivity)。

              Main
              | ^
              V |
      DisplayListActivity (listId is passed with Intent, default=1)
              | ^
              V |
   EditListPropertiesActivity (listId is passed with Intent, default=1)

当我在MainActivity上选择List id = 2,然后在DisplayListActivity上选择属性按钮时,就会出现问题。完成EditListPropertiesActivity的操作后,我在ActionBar上单击“
我返回到DisplayListActivity,但是没有看到列表id = 2,而是看到了id = 1(默认)的列表。

如何将ListId从EditListPropertiesActivity形式传递回DisplayListActivity?
  • startActivityForResult并返回ID-可以正常工作,但是
  • 很丑
  • 使用SharedPreferences并像lastVisitedList一样存储sth-丑陋的
  • 将lastVisitedList信息存储在数据库中-甚至更丑陋的

  • 解决该问题的常用方法是什么?

    下面的代码段。

    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
    @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
    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);
        ...
    

    清单
    <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>
    

    最佳答案

    试试这个完成 Activity

    Intent intent = new Intent();
    intent.putExtra(EXTRA, value);
    setResult(RESULT_OK, output);
    finish();
    

    这是为了获得上一个 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)
    
        }
    }
    

    编辑:阅读评论!需要使用startActivityForResult

    关于java - 使用ActionBar导航回到上一个 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25962311/

    10-12 02:49