Reputation: 9297
The MainActivity
contains some buttons. Each button opens a new activity via an intent. These activities then have a button to return to the MainActivity
via an intent.
But when I press a button to return to the MainActivity
, I get some sort of menu on the screen! Someone who knows what could be wrong? Preciate some help! Thanks!
EDIT: The return button in one of the other activities:
Button btnReturn1 = (Button) findViewById(R.id.btnReturn1);
btnReturn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent returnBtn = new Intent("android.intent.action.MAIN");
startActivity(returnBtn);
}
});
The Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kullaberg.test02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity1"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.ACTIVITY001" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.ACTIVITY002" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity3"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.ACTIVITY003" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Upvotes: 39
Views: 123807
Reputation: 1
I just do this way
public void retorna(View view)
{
Intent muda = new Intent(getApplicationContext(),MainActivity.class);
muda.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(muda);
}
Upvotes: 0
Reputation: 280
This usually works as well :)
navigateUpTo(new Intent(getBaseContext(), MainActivity.class));
Upvotes: 11
Reputation: 131
I'm used it and worked perfectly...
startActivity(new Intent(getApplicationContext(),MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
because Finish() use for 2 activities, not for multiple activities
Upvotes: 13
Reputation: 9975
I highly recommend reading the docs on the Intent.FLAG_ACTIVITY_CLEAR_TOP
flag. Using it will not necessarily go back all the way to the first (main
) activity. The flag will only remove all existing activities up to the activity class given in the Intent
. This is explained well in the docs:
For example, consider a task consisting of the activities: A, B, C, D.
If D calls startActivity() with an Intent that resolves to the component of
activity B, then C and D will be finished and B receive the given Intent,
resulting in the stack now being: A, B.
Note that the activity can set to be moved to the foreground (i.e., clearing all other activities on top of it), and then also being relaunched, or only get onNewIntent()
method called.
Upvotes: 1
Reputation: 11662
Here's why you saw the menu with the code you listed in your onClick
method:
You were creating an Intent with the constructor that takes a string for the action parameter of the Intent
's IntentFilter
. You passed "android.intent.action.MAIN"
as the argument to that constructor, which specifies that the Intent
can be satisfied by any Activity
with an IntentFilter including <action="android.intent.action.MAIN">
.
When you called startActivity
with that Intent
, you effectively told the Android OS to go find an Activity
(in any app installed on the system) that specifies the android.intent.action.MAIN
action. When there are multiple Activities that qualify (and there are in this case since every app will have a main Activity
with an IntentFilter
including the "android.intent.action.MAIN"
action), the OS presents a menu to let the user choose which app to use.
As to the question of how to get back to your main activity, as with most things, it depends on the specifics of your app. While the accepted answer probably worked in your case, I don't think it's the best solution, and it's probably encouraging you to use a non-idiomatic UI in your Android app. If your Button
's onClick()
method contains only a call to finish()
then you should most likely remove the Button
from the UI and just let the user push the hardware/software back button, which has the same functionality and is idiomatic for Android. (You'll often see back Buttons used to emulate the behavior of an iOS UINavigationController navigationBar which is discouraged in Android apps).
If your main activity launches a stack of Activities and you want to provide an easy way to get back to the main activity without repeatedly pressing the back button, then you want to call startActivity
after setting the flag Intent.FLAG_ACTIVITY_CLEAR_TOP
which will close all the Activities in the call stack which are above your main activity and bring your main activity to the top of the call stack. See below (assuming your main activity subclass is called MainActivity:
btnReturn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
)};
Upvotes: 56
Reputation: 1105
use
Intent returnBtn = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(returnBtn);
make the main activity's launchmode
to singleTask
in Android Manifest if you don't want to create new one every time.
android:launchMode="singleTask"
Upvotes: 20
Reputation: 349
Use this code on button click in activity and When return back to another activity just finish previous activity by setting flag in intent then put only one Activity in the Stack and destroy the previous one.
Intent i=new Intent("this","YourClassName.Class");
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Upvotes: 1
Reputation: 3388
why don't you call finish();
when you want to return to MainActivity
btnReturn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
Upvotes: 58
Reputation: 2023
instead of starting MainActivity again via startActivity, call finish() instead in the other activities to get back to MainActivity... as MainActivity is already in stack
Upvotes: 2