I have an activity with a listview. When the user click the item, the item \"viewer\" opens:
List1.setOnItemClickListener(new OnItemClickListener() {
@Ov
Following Steps are much enough to back button:
Step 1: This code should be in Manifest.xml
<activity android:name=".activity.ChildActivity"
android:parentActivityName=".activity.ParentActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.ParentActivity" /></activity>
Step 2: You won't give
finish();
in your Parent Activity while starting Child Activity.
Step 3: If you need to come back to Parent Activity from Child Activity, Then you just give this code for Child Activity.
startActivity(new Intent(ParentActivity.this, ChildActivity.class));
AndroidManifest file:
<activity android:name=".activity.DetailsActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="br.com.halyson.materialdesign.activity.HomeActivity" />
</activity>
add in DetailsActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
it's work :]
https://stackoverflow.com/a/46903870/4489222
To achieved this, there are simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);}
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
in onCreated
method for the new apis.
Make sure your the ActionBar Home Button is enabled in the Activity:
@Override
public void onBackPressed() {
...
super.onBackPressed();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Example MainActivity
that extends ActionBarActivity
:
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// API 5+ solution
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
This way all the activities you want can have the backpress.
http://developer.android.com/training/implementing-navigation/ancestral.html
AndroidManifest.xml
:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Example MainActivity
that extends ActionBarActivity
:
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
@OptionsItem(android.R.id.home)
void homeSelected() {
onBackPressed();
}
Source: https://github.com/excilys/androidannotations