Reputation: 3069
I've tried to make a "return" button for my app, or as Google call it, Up Button for Low-level Activities.
I wrote everything properly: in My XML I put the meta-data and the parent activity (Example from Google, I modified the text to my app in my files)
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
in My class I put the next line:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
but when I click this up button, the app closes, then opened again from the main screen.
I want it to return to the main screen without closing (finishing) the app.
Can you help me? Thanks.
Upvotes: 1
Views: 4450
Reputation: 6892
Well, according to this guide,
When running on Android 4.1 (API level 16) or higher, or when using ActionBarActivity from the Support Library, performing Up navigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.
So, I have two Activities
: FirstActivity, which is my launcher and parent Activity
and SecondActivity, which is my child Activity
.
My FirstActivity code:
public class FirstActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
Button secondButton = (Button) findViewById(R.id.secondButton);
secondButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
My SecondActivity code:
public class SecondActivity extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_place_activity);
//enable the ActionBar behaviour
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
My Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.testes.activity.FirstActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.testes.activity.SecondActivity"
android:parentActivityName="com.testes.activity.FirstActivity" >
</activity>
</application>
</manifest>
And this is all I need to have it working your way. I start my FirstActivity
, click my Button to go to the SecondActivity
, click the ActionBar
home button and it goes back to FirstActivity
.
Upvotes: 1