开发者

A way of knowing what activity will be launched when BACK is pressed in Android

I need to know if there is any way to know when on an Activity what is the Activity that will be opened when the back button is 开发者_Go百科pressed. I suppose i can take a look at the activity stack but i need some pointer on how to do it.

Thanks in advance.

EDIT: Thank for all the answers but i still want to explain the real problem.

I have an App that has a bunch of activities that consume a lot of power (sensor, gps and wifi), that i want to keep quiet when i'm not using that 'Task' (i.e going to do something else).

How can i trap the event of not having nothing more of my application in the back-stack?


Send along an intent extra from ActivityX to ActivityY. The extra could be a reference to a constant value which identifies an activity.

ActivityX:

startActivity(new Intent(context, ActivityY.class).putExtra("fromActivity", Const.EXTRA_FROM_ACTIVITY_X));

ActivityYB:

private int fromActivity;
public void onCreate(Bundle savedInstanceState) {
    ...
        fromActivity = getIntent().getExtras().getInt("fromActivity");
    ...
}
public void onBackPressed() {
    switch(fromActivity) {
        case Const.EXTRA_FROM_ACTIVITY_X:
            //we are going back to ActivityX
            break;
    }
}

Where Const is a class holding unique static final int variables such as EXTRA_FROM_ACTIVITY_X


if you can guess which activity, then you can use the instanceof method in a conditional statement

@Override
public void onBackPressed(){
Context mycontext = this;
if(mycontext instanceof className)
{ 
   startActivity(new Intent(this, distinatinClass);
}
else if(mycontext instanceof differentClassName)
....
else
....
}


see this task design for android.

this should help as well.

various APIs ,variables are available:

taskAffinity

launchMode

allowTaskReparenting

clearTaskOnLaunch

alwaysRetainTaskState

finishOnTaskLaunch


I think you should better describe your problem with more details to let us help you find the best solution, instead of trying to find a way to implement the solution you have chosen and which looks bad because it violates principles & assumptions of Android OS.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜