简体   繁体   中英

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.

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.

I suggest creating an Application class. In that class have a boolean field that is false by default. 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() ). You can even create a custom Activity that does this and then have all activities extend that activity.

Resources:

Stripped-down example:

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

[...] <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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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