同一堆栈的活动之间的通信

时间:2015-09-17 21:57:17

标签: android android-intent back-stack

我想要一个接受onCreate数据并返回数据onDestroy的活动;所以我写了一个小applet(dex文件仍然被认为是applet,或者几乎所有的名字都被命名为#34; app" now)来完善这个想法:

清单摘录

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="app_label" >
    <activity
        android:name=".MainActivity"
        android:label="app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ChildActivity"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
</application>

MainActivity.java:

package com.example.tests;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
    public static final String EXTRA_LABEL = "extra";
    private EditText text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        text = new EditText(this);
        text.setText("initial value");
        Button button = new Button(this);
        button.setText("call");
        final MainActivity parent = this;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity child = new ChildActivity();
                Intent intent = Intents.addStackActivity(parent, child);
                intent.putExtra(EXTRA_LABEL, text.getText().toString());
                startActivity(intent);
            }
        });
        layout.addView(button);
        layout.addView(text);
        this.setContentView(layout);
    }
    @Override public void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ChildActivity.EXTRA_LABEL_CODE) {
             if (resultCode == RESULT_OK) {
                 runOnUiThread(new Runnable() {
                     @Override public void run() {
                         text.setText(data.getStringExtra(ChildActivity.EXTRA_LABEL));
                     }
                 });
             }
        }
    }
}

Intents.java:

package com.example.tests;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class Intents {
    public static Intent addStackActivity(Context parent, Activity activity) {
        int id = 1;

        Intent resultIntent = new Intent(parent, activity.getClass());
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(parent);
        // Adds the back stack
        stackBuilder.addParentStack(activity.getClass());
        // Adds the Intent to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        // Gets a PendingIntent containing the entire back stack
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                parent);
        builder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) parent
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(id, builder.build());
        return resultIntent;
    }
}

ClientActivity.java:

package com.example.tests;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ChildActivity extends Activity {
    public static final String EXTRA_LABEL = "mwahahaha";
    public static final int EXTRA_LABEL_CODE = 12345678;
    private EditText text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String data = intent.getExtras().getString(MainActivity.EXTRA_LABEL);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        text = new EditText(this);
        text.setText(data);
        Button button = new Button(this);
        button.setText("return");
        button.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                finish();
            }
        });
        layout.addView(button);
        layout.addView(text);
        this.setContentView(layout);
        //setContentView(R.layout.activity_main);
    }
    private void setTextResult() {
        Intent intent = new Intent();
        intent.putExtra(ChildActivity.EXTRA_LABEL, text.getText().toString());
        this.setResult(ChildActivity.EXTRA_LABEL_CODE, intent);
    }
    @Override public void onDestroy() {
        setTextResult();
        super.onDestroy();
    }
}

子活动正在接受字符串就好了;但是我很难获得将数据返回给父级的方法。这是怎么做到的?

1 个答案:

答案 0 :(得分:3)

我并不完全清楚你想要完成什么,但我怀疑你可以用“startActivityForResult”取代一大堆自定义代码。

请参阅http://developer.android.com/training/basics/intents/result.html