开发者

Returning to previous screen after receiver activity

I have a app that has a activity that can be started from the start menu and a activity that is started by a broadcast receiver.

Returning to previous screen after receiver activity

Now on boot , if the receiver is called , the "event viewer" activity is started...after backing out of this activity user returns to previous screen (could be homescreen or whatever user was doing) .. this is how it should be.

But if i start the "main" activity from the main launcher, and press the home button to go back to the home screen..the problem begins. Now if the receiver is called the "event viewer" activity is shown. if the user backs out of the "event viewer" activity (or i call finish() ) it will show the "main" activity(still running in background) instead of the previous thing the user was doing (like home screen).

This is not how i want it..because it causes users after dismissing a calendar event (the purpose of my app) to return to eg the settings from the main app..

If i call finish() in onpause , it works ok...but that is n开发者_运维百科ot the way it should work.

Any clues?

Hope the problem is clear , since english isn't my first language i found it hard to explain the problem :-)

Thanks..


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 a taskAffinity for your IndependentActivity.

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

I have excluded it from the Recents but that is totally optional.


It's generally unwise in my opinion to launch activities unless the user has elected to run the app (widget press, notification select, launcher, etc). Your example is just one reason why suddenly changing the activity when the user isn't expecting it might cause problems.

If the user isn't using the app the best way to get their attention is through the status bar notification system.


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 call yourOtherActivity.this.finish()

I think it's that. What's your first language?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜