如何管理“startActivityForResult”'和' ActivityResult'在(A,B)活动中?

时间:2017-03-02 10:03:38

标签: android android-intent startactivityforresult

我有两个活动A和B. 从A开始,我通过startActivityForResult向B发送一些数据,并在ActivityResult中收到来自B的回复。 现在,如果我想处理B发送给我然后将数据发送回B的内容,我是否必须在B中使用startActivityForResultActivityResult

它在A和B之间进行聊天(来回)。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

我编写了使用LocalBroadcastReceiveractivities之间发送数据的代码 活动A:

    public class A extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        // Registering LocalBroadcastReceiver to receiver data from B
        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiverA, new IntentFilter("activity_a"));
        findViewById(R.id.btnStartB).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(A.this, B.class);
                i.putExtra("key_string", "Value"); // add stuffs to be sent to Activity B
                startActivity(i);
            }
        });
    }

    @Override
    protected void onDestroy() {
        // Don't forget to unregister receiver
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiverA);
        super.onDestroy();
    }

    BroadcastReceiver mReceiverA = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, Intent intent) {
            // Get data from Intent, which we got from Activity B
            if (intent.hasExtra("key_string"))  {
                Toast.makeText(context, intent.getStringExtra("key_string"), Toast.LENGTH_SHORT).show();
                // Here you can process the data, and send result to Activity B
                // Adding delay temporary, so that Toast will be visible on by one
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent i = new Intent("activity_b");
                        i.putExtra("key_string", "Value Of String From A");
                        LocalBroadcastManager.getInstance(context).sendBroadcast(i);
                    }
                }, 600);
            }
        }
    };
}


活动B:

public class B extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

        // Registering LocalBroadcastReceiver to receiver data from A
        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiverB, new IntentFilter("activity_b"));

        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent("activity_a");
                i.putExtra("key_string", "Value Of String From B");
                LocalBroadcastManager.getInstance(B.this).sendBroadcast(i);
            }
        });
    }

    @Override
    protected void onDestroy() {
        // Don't forget to unregister receiver
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiverB);
        super.onDestroy();
    }

    BroadcastReceiver mReceiverB = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get data from Intent, which we got from Activity B
            if (intent.hasExtra("key_string"))  {
                Toast.makeText(context, intent.getStringExtra("key_string"), Toast.LENGTH_SHORT).show();
            }
        }
    };
}

希望这会有所帮助: - )

答案 1 :(得分:0)

var request = this.listPartners.filterChange.asObservable()
    .debounceTime(300)
    .mergeMap(value =>
    {
        if (value != "")
            return this.partnersService.list({ filter: value }) //call http get
        return;
    });
if (request)
    request.subscribe(partners => this.partners = partners);

在第一个活动中,

reverseStr = '';
fprintf('Percent done: ');
for idx = 1 : someLargeNumber
   % Do some computation here...
   % Display the progress
   percentDone = 100 * idx / someLargeNumber;
   msg = sprintf('%3.1f', percentDone); %Don't forget this semicolon
   fprintf([reverseStr, msg]);
   reverseStr = repmat(sprintf('\b'), 1, length(msg));
end

,在你完成第二项活动时,

Intent yourFirstAct= new Intent(firstAct.this,second.class);
yourFirstAct.putExtra("","");
startActivitForResult(yourFirstAct);

如果你以片段方式执行startActivityResult(),

你的片段mainActivity必须在

中返回super()
public void onAcitivityResult(....){
 super();
}