2016-01-03

【Android Studio】回傳資料給 Parent Activity

Honeycomb 3.0(API 11) 之前,Parent Activity 若呼叫 Child Activityuser 按了 Android 裝置上的硬體返回鍵,就會離開 Child Activity 返回 Parent Activity,並會觸發 Parent Activity 的 onActivityResult() 並將資料回傳給 Parent Activity

現在有了 Action BarAction Bar 內也有返回鍵,但 user 按了 Action Bar 的返回鍵,並不會觸發 Parent Activity 的 onActivityResult(),要稍微改變一下寫法,才能觸發 Parent ActivityonActivityResult()


MainActivity.java 內容:
 
public class MainActivity extends AppCompatActivity
{
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View v)
    {
        Intent intentChild = new Intent(this, ChildActivity.class);
        startActivityForResult(intentChild,0);    // 呼叫 child activity,並要求回傳資料
    }

    // 接收 child activity 傳回的資料
    protected void onActivityResult(int requestCode, final int resultCode, final Intent data)
    {
        if(data!=null && resultCode==RESULT_OK)
        {
            Bundle bData = data.getExtras();
            if(bData!=null)
            {
                String childactivity = bData.getString("activity");
                if(childactivity.equals("ChildActivity"))
                {
                    String answer = bData.getString("answer");
                    Toast.makeText(this,"answer is "+answer,Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
 


activity_main.xml 的內容:
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.childtest.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Child Activity"
        android:id="@+id/button"
        android:onClick="onClick"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>
 


menu_child.xml 的內容:
 
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ChildActivity">

    <item
        android:id="@+id/action_save"
        android:title="存檔"
        android:orderInCategory="100"
        app:showAsAction="ifRoom" />
</menu>
 


ChildActivity.java 內容:
 
public class ChildActivity extends AppCompatActivity
{
    private static final String TAG = "ChildActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_child, menu);
        return true;
    }

    @Override
    public void onBackPressed()
    {   // 按了 Android 裝置的實體返回鍵
        // 準備回傳的資料
        Bundle bData = new Bundle();
        bData.putString("activity", TAG);
        bData.putString("answer", "Good job");
        Intent mIntent = new Intent();
        mIntent.putExtras(bData);
        setResult(RESULT_OK, mIntent);
        super.onBackPressed();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        switch(id)
        {
            case android.R.id.home: // 按了 Action Bar 的返回鍵
                onBackPressed();
                return true;    // 注意! 一定要回傳 true
            case R.id.action_save:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
 

相關筆記 ----
【Android】設定 Support Library
【Android】啟用 Support Library 的 ActionBar
【Android】ActionBar 加入返回鍵 


1 則留言:

  1. 謝謝版主,參考您的方法後,問題已經解決。

    回覆刪除