1

I am starting a new intent:

Intent intent = new Intent();
intent.setComponent(new ComponentName("xx.xxxxxx.xxxx", "xx.xxxxxx.xxxx.Activity"));
startActivityForResult(intent, 1);

But sometimes new application have some error and I must kill them, and I must turn it again.

I tried to add:

intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK & Intent.FLAG_ACTIVITY_NEW_TASK);

but it's not working.

[SOLUTION]:

Intent intent = new Intent();
intent.setComponent(new ComponentName("xx.xxxxxx.xxxx", "xx.xxxxxx.xxxx.Activity"));
startActivityForResult(intent, 1);

MainActivity
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK & Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

SecondActivity
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("xx.xxxxxx.xxxx", "xx.xxxxxx.xxxx.Activity"));
    startActivityForResult(intent, 1);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
        // saveInSharedPreferences...
            finish();
    }

MainActivity
    onResume() {
        getWithSharedPreferences...
    }
Pionas
  • 346
  • 1
  • 4
  • 15

1 Answers1

1

You can't do this. If you want to start another Activity using startActivityForResult() you cannot use Intent.FLAG_ACTIVITY_NEW_TASK. When starting an Activity that will return a result, the target Activity must run in the same task.

There isn't much you can do about this.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks. Maybe can I create a new activity in the new task? And there I will add the above code? When I get an answer I finish new activity and send answer to MainActivity – Pionas Aug 19 '16 at 10:57
  • Sure, you could do that. It might confuse your users though. And you can't use `startActivityForResult()` to send the result back to your `MainActivity`. You'll need to use a `BroadcastReceiver` or `SharedPreferences` or something like that to communicate between the 2 tasks. – David Wasser Aug 19 '16 at 11:32