简体   繁体   English

android中的应用主屏幕?

[英]Going Home Screen of app in android?

I want to go to homePage of my application on clicking button where i was in inner page. 我想在我位于内页的按钮上转到应用程序的主页。 Can any one tell me how to do that ? 谁能告诉我该怎么做?

Thanking you , 感谢您 ,

Srinivas 斯里尼瓦斯

在您的按钮单击事件上,添加以下代码行

moveTaskToBack(true);

There are probably two ways of doing that (general concepts): 可能有两种方法(一般概念):

The first one would be to simply launch the home actvity again, if you don't mind having it re-created again, unless that activity is a "singleTask" or "singleInstance" activity. 第一个方法是,如果您不介意再次重新创建家庭活动,则只需再次启动它,除非该活动是“ singleTask”或“ singleInstance”活动。

The second one would be to close the top activity in the stack as long as it is not your home activity. 第二个方法是关闭堆栈顶部的活动,只要它不是您的家庭活动即可。 I don't see an easy way to achieve that, maybe by finishing the current activity with a specific result that gets checked by the launching activity, who will in turn close and send the result until the home activity is reached. 我看不到一种简单的方法来实现此目的,也许是通过用启动活动检查的特定结果完成当前活动,然后启动活动将检查该特定结果,然后关闭该结果并将其发送,直到达到home活​​动为止。

I suggest creating an Application class. 我建议创建一个Application类。 In that class have a boolean field that is false by default. 在该类中,有一个布尔字段,默认情况下为false。 Every Activity should check if that field is true in onResume() and call finish() if it is true (except for the main activity that always sets the field to false in onResume() ). 每个Activity都应检查onResume()该字段是否为true,如果为true,则调用finish() (主活动除外,该活动总是在onResume()将该字段设置为false )。 You can even create a custom Activity that does this and then have all activities extend that activity. 您甚至可以创建一个自定义Activity来执行此操作,然后让所有活动扩展该活动。

Resources: 资源:

Stripped-down example: 精简示例:

MyApp MyApp

public class MyApp extends Application { public boolean goBack = false; }

MyActivity 我的活动

public class MyActivity extends Activity {
    protected void onResume() {
        if ( ((MyApp) getApplication()).goBack ) finish();
    }
}

SomeActivity 一些活动

public class SomeActivity extends MyActivity {
    // nothing special here, it's all been implemented in MyActivity!
}

MainActivity 主要活动

public class MainActivity extends Activity {
    protected void onResume() {
        ((MyApp) getApplication()).goBack = false;
    }
}

AndroidManifest.xml AndroidManifest.xml

[...] <application android:name=".MyApp" [...]> [...]

Note: You don't need to declare MyActivity in AndroidManifest.xml because it will never be launched directly (it will only be extended). 注意:您不需要在AndroidManifest.xml声明MyActivity ,因为它永远不会直接启动(只会被扩展)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM