Search code examples
androidonactivityresult

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>

Solution

  • 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  
                }  
            });