外部应用无效的结果的StartActivity

时间:2015-12-28 05:18:59

标签: android android-intent startactivityforresult

我准备2个应用程序将其命名为App-1& App-2,使用App-1我从StartActivityForResult()调用App-2的MainActivity。直到它工作正常,我能够调用App-2 MainActivity。现在进行计算,App-2在内部调用App-2的SecondActivity。所以此时app开始破解。在没有等待SecondActivity的内部响应的情况下,MainActivity以dataCode = 0响应并且数据中的null隐式地响应我们的App-1(我甚至没有调用setResult())。

我不能直接回应MainActivity,强制我需要使用app-2中的SecondActivity然后返回到App-2 MainActivity并回复App-1。

帮我解决这个问题。 (因为我的App-2是包含在App-1中的.aar库,我无法调试它)。下面是它的代码。

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

来电者活动 - 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();
    }
}

0 个答案:

没有答案