3

i need to restart my main activity from background Service.
I get the results of my service by a BroadcastReceiver and I need to resume it (after some operations).

Is it possible ?
How can i do this ?

I tried this solution Resume application and stack from notification but it doesn't work for me.

EDIT:
Thats my receiver:

private BroadcastReceiver receiver = new BroadcastReceiver() {


        @Override
        public void onReceive(Context context, Intent intent) {
          Bundle bundle = intent.getExtras();
          if (bundle != null) {
            int resultCode = bundle.getInt(DownloadService.RESULT);
            if (resultCode == RESULT_OK) {
                Log.i("MyApp", "RESULT OK");
                final Intent mainIntent = new Intent(context, MainActivity.class);
                mainIntent.setAction(Intent.ACTION_MAIN);
                mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(mainIntent);
            } else {
              Toast.makeText(MainActivity.this, "KO",Toast.LENGTH_LONG).show();
            }
          }
        }

 };
Community
  • 1
  • 1
enfix
  • 6,680
  • 12
  • 55
  • 80
  • What do you mean it doesn't work for you? Do you get exceptions? Do you need the app to come Into the foreground and completely interrupt what the user was doing? – Matt Wolfe Oct 30 '13 at 00:38
  • No errors. The app didn't resume and I'm sure the service are running because I have some logs. – enfix Oct 30 '13 at 14:47
  • Post some code so we can see what you are doing. Tons of apps launch from notification including my own. This definitely works and is possible on android. – Matt Wolfe Oct 30 '13 at 15:44

2 Answers2

6
Intent intent = new Intent(context.getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
context.getApplicationContext().startActivity(intent);
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
Haroon Ahmed
  • 227
  • 3
  • 4
4

You must use this flag with your Intent:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

IF you don't want a new instance of your Activity, set launchMode for your Activity in the AndroidManifest file:

android:launchMode="singleTask"
Pelanes
  • 3,451
  • 1
  • 34
  • 32