开发者

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 开发者_C百科one tell me how to do that ?

Thanking you ,

Srinivas


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:

  • http://d.android.com/resources/faq/framework.html#3

    The android.app.Application class

    The android.app.Application is a base class for those who need to maintain global application state. It can be accessed via getApplication() from any Activity or Service. It has a couple of life-cycle methods and will be instantiated by Android automatically if your register it in AndroidManifest.xml.

  • Reference: http://d.android.com/reference/android/app/Application.html

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


On your button click event add the following lines of code

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜