简体   繁体   中英

Calling startActivityForResult never pass to onActivityResult

I'm calling startActivityForResult from a child activity to MainActivity but never pass for onActivityResult

secondActivty this is the call

private void bringMainActivityToTop() {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    Bundle bun= new Bundle();
    bun.putParcelable("value",new ListData("HelloDummy",longi,lati,"data"));
    intent.putExtra("bundle",bun);
    setResult(RESULT_OK, intent);
    startActivityForResult(intent, 111);
}

And I want see the call in here on the MainActivity I don't care the code I just want to see the call.

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
    super.onActivityResult(requestCode, responseCode, data);
    System.out.println("HelloWorld");
    Log.i(TAG,"HelloWorld");

}

and the manifest

 <activity
        android:name="com.test.mppqvat.activity.MainActivity"
        android:label="@string/main_act_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

onActivityResult() belongs to your second activity (if its not then it must to), not the MainActivity . onActivityResult() method wont trigger in MainActivity just cuz MainActivity is started for result. Do you realize that?
Also your MainActivity if being started for result must SET the result using setResult() method and than it has to be finished using finish() method, that way the result will be passed from your MainActivity to your caller SeconActivity activity and its onActivityResult() will be fired.

您调用的Activity必须调用setResult(Activity.RESULT_OK)setResult(Activity.RESULT_OK, data)

MainActivity.java(in onCreate)

button1=(Button)findViewById(R.id.button1);  
    button1.setOnClickListener(new OnClickListener() {  
        @Override  
        public void onClick(View arg0) {  
            Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
            startActivityForResult(intent, 2);// Activity is started with requestCode 2  
        }  
    });  

(outside onCreate)

   @Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data)  
   {  
             super.onActivityResult(requestCode, resultCode, data);  
              // check if the request code is same as what is passed  here it is 2  
               if(requestCode==2)  
                     {  
                        String message=data.getStringExtra("MESSAGE");   
                        //use resulted message  
                     }  
 }  

SecondActivity.java(in onCreate)

button1=(Button)findViewById(R.id.button1);  
        button1.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View arg0) {  
                String message=editText1.getText().toString();  
                Intent intent=new Intent();  
                intent.putExtra("MESSAGE",message);  
                setResult(2,intent);  
                finish();//finishing activity  
            }  
        });  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM