一种了解 Android 中按下 BACK 时将启动什么 Activity 的方法

发布于 2024-11-02 03:07:25 字数 229 浏览 0 评论 0原文

我需要知道是否有任何方法可以知道在活动上按下后退按钮时将打开的活动是什么。我想我可以查看活动堆栈,但我需要一些关于如何执行此操作的指示。

提前致谢。

编辑:感谢所有答案,但我仍然想解释真正的问题。

我有一个应用程序,它有很多消耗大量电量的活动(传感器、GPS 和 WiFi),当我不使用该“任务”(即要做其他事情)时,我想保持安静。

我如何捕获后台堆栈中不再有我的应用程序的事件?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳哥哥 2024-11-09 03:07:25

将 Intent Extra 从 ActivityX 发送到 ActivityY。 extra 可以是对标识活动的常量值的引用。

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;
    }
}

其中Const 是一个包含唯一的 static Final int 变量的类,例如 EXTRA_FROM_ACTIVITY_X

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

我三岁 2024-11-09 03:07:25

如果您可以猜测哪个活动,那么您可以在条件语句中使用instanceof方法

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

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
....
}
风吹雪碎 2024-11-09 03:07:25

查看此 Android 任务设计

这应该也会有所帮助

变量可用

taskAffinity

launchModeallowTaskReparentingclearTaskOnLaunchalwaysRetainTaskState

各种

finishOnTaskLaunch

API ,

see this task design for android.

this should help as well.

various APIs ,variables are available:

taskAffinity

launchMode

allowTaskReparenting

clearTaskOnLaunch

alwaysRetainTaskState

finishOnTaskLaunch

◇流星雨 2024-11-09 03:07:25

我认为你应该更好地用更多细节描述你的问题,让我们帮助你找到最好的解决方案,而不是试图找到一种方法来实现你所选择的解决方案,这看起来很糟糕,因为它违反了原则和原则。 Android 操作系统的假设。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文