StartActivity for Result for external App not work

2019-09-14 01:53发布

问题:

I am preparing 2 Apps name it App-1 & App-2, using App-1 I call MainActivity of App-2 from StartActivityForResult(). Till this it works fine, I am able to call App-2 MainActivity. Now for calculations App-2 internally calls SecondActivity of App-2. So at this point app starts breaking. Without waiting for internal response from SecondActivity, MainActivity responds with resultCode=0 and null in data to our App-1 implicitly (I haven't even called setResult()).

And I cannot directly respond from MainActivity, compulsory I need to use SecondActivity from app-2 then get back to App-2 MainActivity and Respond to App-1.

Help me out with this. ( since my App-2 is .aar library included in App-1, I cannot debug into it ). Below is the code for it.

MainActivity.java - App2

public static int flag = 0;
public static String dst = "Location - 23.029744, 72.541593";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    if(flag == 0)
    {
        Intent in = new Intent(this,SecondActivity.class);
        startActivity(in);
    }

}


@Override
protected void onResume() {
    super.onResume();


    if(flag != 0)
    {
        Intent inr = new Intent();
        inr.putExtra("latitude", dst);
        setResult(2, inr);
        flag = 0;
        finish();
    }


}

SecondActivity - App2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

} 
//Invoked by button click
public void btn_click(View v)
{
    MainActivity.dst = "Value obtained from API";
    MainActivity.flag = 1;
    Intent intent=new Intent(this,MainActivity.class);
    startActivity(intent);
    finish();
}

Caller Activity - App-1

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}


public void btn_click(View v)
{
    Intent intent=new Intent(MainActivity.this,com.nestedif.sdk.MainActivity.class);
    startActivityForResult(intent, 2);// Activity is started with requestCode 2
}


@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(resultCode==2)
    {
        String message=data.getStringExtra("latitude");
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
}