Android Studio - end activity from IntentService

0

I have an activity that initiates an IntentService which verifies a code that I entered and if it is correct it sends me to a new activity always from the IntentService, then a new activity starts but the previous one I can not finish it because I can not do it a finish () from the IntentService.

    
asked by Daylight Ark 23.05.2018 в 16:09
source

2 answers

0

Thanks for your response, I was able to solve my problem by changing the 3 Intent code lines for these:

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);  

In this way the application is restarted and can be verified, which is the problem I had with that tutorial:
link

Thank you very much for your help!

    
answered by 23.05.2018 / 17:35
source
1

What I would do, without knowing exactly how that code works, try and see if it is what you need or not.

I would not start the activity in IntentService , I would do the following:

Where you now start the activity, you would call a broadcast

Intent intent = new Intent("cerrarActivity");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

And then in the activity that you want to close:

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
          // aqui abres la nueva activity y cierras la actual
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }
    };

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

        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("cerrarActivity")); // register broadcast

    }

    @Override
    public void onPause() {
        super.onPause();

        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); // unregister broadcast
    }
    
answered by 23.05.2018 в 16:43