views:

43

answers:

3

Hai ,

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

A: 

On your button click event add the following lines of code

moveTaskToBack(true);

Rahul
Its going to homescreen , but i need to swith to homePage of my app i.e; to MainActivity.
Srinivas
Oops i am sorry i did not read your question properly.... I think you already got a partial answer for the same. You can keep your parent activity as singleTask and when you call the home activity from somewhere else set this flag Intent.FLAG_ACTIVITY_CLEAR_TOP. So all activities inbetween them will get finished
Rahul
A: 

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.

SirDarius
I wouldn't recommend the first "solution", as that will create an activity loop (main -> other -> main -> other -> main) which can be pretty annoying.
Felix
i tried 1st solution , as u said , loop formed , when i clicked back .
Srinivas
Please can u tell me how to proceed with second solution ?
Srinivas
if you call startActivityForResult(), you can expect a return value from the launched activity if you override onActivityResult(). You can then check whether you should close the current activity based on the returned value.
SirDarius
A: 

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

Felix
Thank you dude .for solution.
Srinivas