开发者

Android: keep alert in front, so that the user must respond

My application shows an alert that the user must respond to before continuing to do other things. I'm trying to figure out the best way to implement this. Using an Activity for the alert isn't quite working.

In my current implementation, the 开发者_StackOverflow社区alert is activity (A). When another activity from the same package is started and onStop is called, it starts itself again using FLAG_ACTIVITY_REORDER_TO_FRONT so that it's always at the top of the stack. This works as described, unless Activity A uses Theme.Dialog or Theme.Translucent.

Modified log:

Activity A created
Activity A started
Activity A resumed
Activity A paused
Activity B created
Activity B started
Activity B resumed
Activity B gains window focus
Activity A stopped
Top activity in stack is Activity B, so Activity A relaunches itself
Activity B paused
Activity A started
Activity A resumed

The top activity in the stack should be Activity A, however Activity B remains in the foreground.

Another implementation detail: my application is not for a phone, so I'm not concerned with a back button finishing the activity or interactions with other apps. Still, I agree that on principle I should prevent such problems anyway, so in my code I check whether the activity that has come in front is from the same package (i.e. from our code base). This should work around the theoretical problem of interfering with other apps.

Is there a way to bring Activity A into focus? I understand that this is unusual behavior, but it is necessary for Activity A to remain in the foreground until it is deliberately finished.

I'm also open to suggestions about a completely different and better approach!

FWIW, I'm running 2.2.

(Cross-posted from http://groups.google.com/group/android-developers/browse_thread/thread/d46fd7d59abe15a0, where we got no response.)


You can't do this. Please don't do this. The activity at the top of the stack is the one that has input focus. What you are trying to do fundamentally breaks the user interaction that is supposed to happen.

What you are doing is generally considered by the platform to be an abuse of it, and Android has increasingly been doing things to prevent applications like this from causing harm.


Well, here's what I had in mind:

public class ActivityA extends Activity
{

    ...

    public void onStop() {
        super.onStop();
        finish();
        Intent i = new Intent();
        i.setClass(getApplicationContext(), ActivityA.class);
        startActivity(i);
    }
}

ActivityA is finished in onStop() and started again right away. You might have to check issues regarding device rotation, but this approach should work.


Having window focus means that activity B is still in its visible lifetime, since it has on top the activity A which has a translucent bg or is dialog-like.

Having the window focus doesn't mean that activity B is on the foreground. They are different things.

If you don't want this, then don't use those two themes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜