Returning to the previous screen after a receiver action

I have an application that has an activity that can be started from the very beginning of a menu and an activity that is triggered by a broadcast receiver.

enter image description here

Now on boot, if the receiver is called, the "event viewer" activity started ... after returning from this activity, the user returns to the previous screen (can be on the desktop or anywhere else). This is how it should be done.

But if I start the "main" activity from the main launcher and press the home button to return to the main screen. The problem begins. Now if the receiver is called an activity "Event Viewer" is displayed. if the user returns from the "event viewer" activity (or I end (), it will show the "main" activity (still running in the background) instead of the previous thing the user was doing (like the home screen).

This is not how I want it ... because it forces users, after dismissing a calendar event (the target of my application), to go back, for example, to settings from the main application.

If I call finish () in onpause it works fine ... but that's not how it should work.

Any hints?

Hope the problem is clear as English is not my first language i found it difficult to explain the problem :-)

Thank..

0


source to share


3 answers


In your receiver, use the flag FLAG_ACTIVITY_NEW_TASK

@Override
public void onReceive(Context context, Intent intent) {
    //... bla bla bla

    Intent in = new Intent(context, IndependentActivity.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(in);

    //... bla bla bla
}

      

In your manifest, provide an Affinity task for your IndependentActivity.



    <activity
        android:name=".IndependentActivity"
        android:excludeFromRecents="true"
        android:taskAffinity="com.example.independantactivty.ind" >
    </activity>

      

I've excluded it from Recents, but it's completely optional.

+3


source


It is generally unwise, in my opinion, to trigger actions if the user has not opted to launch the application (click widgets, select notifications, launcher, etc.). Your example is just one reason why a sudden change in activity when the user is not expecting can cause problems.



If the user is not using the app, the best way to grab attention is through the status bar notification system.

0


source


put

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1000) {
        if (resultCode == 1) {
            Main.this.finish();
        }

    }}

      

in your main activity and in your other activity, put this.setResult (1); before calling yourOtherActivity.this.finish ()

I think so. What's your first language?

0


source







All Articles