In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, it doesn't do that. How do I make that icon take you back to the previous screen?
问题:
回答1:
There are two simple steps to create a back button in the title bar:
First, make the application icon clickable using the following code in the activity whose title bar you want to have a back button in:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
After you have added the above code, you will see a back arrow appear to the left of the application icon.
Second, after you have done the above, you still have to create code that will take advantage of the click event. To do so, be aware that, when you actually click on the application icon, an onOptionsItemSelected
method is called. So to go back to the previous activity, add that method to your activity and put Intent
code in it that will return you to the previous activity. For example, let's say the activity you are trying to go back to is called MyActivity
. To go back to it, write the method as follows:
public boolean onOptionsItemSelected(MenuItem item){
Intent myIntent = new Intent(getApplicationContext(), MyActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
That's it!
(In the Android developers API, it recommends messing around with the manifest and adding stuff like android:parentActivityName
. But that doesn't seem to work for me. The above is simpler and more reliable.)
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
And in your Activity
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
回答2:
use this code
@Override
public void onCreate(Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
after that write this code in onOptionsItemSelected
method
int id = item.getItemId();
if (id==android.R.id.home) {
finish();
}
回答3:
I have finally managed to properly add back button to actionbar/toolbar
@Override
public void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
回答4:
1.- Add the activity to AndroidManifest.xml and make sure to provide the meta-data:
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
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" />
</activity>
2.- Add the following code to the onCreate method of the activity:
@Override
public void onCreate(Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
3.- Override the onOptionsItemSelected and use NavUtils.navigateUpFromSameTask() static method to navigate throw the stack.
@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);
}
However, using navigateUpFromSameTask() is suitable only when your app is the owner of the current task (that is, the user began this task from your app). If that's not true and your activity was started in a task that belongs to a different app, then navigating Up should create a new task that belongs to your app, which requires that you create a new back stack.
回答5:
If your activity does extend Activity
public class YourActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xxx);
getActionBar().setHomeButtonEnabled(true);
[...]
}
[...]
}
If your action extends AppCompatActivity
public class YourActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xxx);
getSupportActionBar().setHomeButtonEnabled(true);
[...]
}
[...]
}
Nothing more to do, See Add up action
[OPTIONAL] To explicitly define parent activity modify your Manifest.xml like this:
<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.YourActivity "
android:label="@string/title_activity_display_message"
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" />
</activity>
</application>
See Specify the Parent Activity
回答6:
The simplest way and best practice as google explains in here :
1.Add a parent for your childActivity in the AndroidManifest.xml
:
<activity
android:name=".ChildActivity"
android:parentActivityName=".ParentActivity" >
</activity>
2.Activate the back button in your childActivity :
myActionOrActionSupportBar.setDisplayHomeAsUpEnabled(true);
Worked for me, I hope it works for you too.
回答7:
If you are using the new support library for 5.1 in android studio, you can instead use this on your AppCompatActivity
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.mipmap.ic_arrow_back_white_24dp);
actionBar.setDisplayShowHomeEnabled(true);
cheers.
回答8:
first of all in onCreate Function add the following line
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and then add the following function in the code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
回答9:
Light-weighted version without using ActionBarActivity
that still has the same bahaviors here:
public class ToolbarConfigurer implements View.OnClickListener {
private Activity activity;
public ToolbarConfigurer(Activity activity, Toolbar toolbar, boolean displayHomeAsUpEnabled) {
toolbar.setTitle((this.activity = activity).getTitle());
if (!displayHomeAsUpEnabled) return;
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(this);
}
@Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(activity);
}
}
Usage: Put new ToolbarConfigurer(this, (Toolbar) findViewById(R.id.my_awesome_toolbar), true);
in onCreate
.
回答10:
If your activity extends AppCompatActivity
you need to override the onSupportNavigateUp()
method like so:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
...
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
Handle your logic in your onBackPressed()
method and just call that method in onSupportNavigateUp()
so the back button on the phone and the arrow on the toolbar do the same thing.
回答11:
If you are using an ActionBar you're going to want to read up on this documentation http://developer.android.com/reference/android/app/ActionBar.html#setDisplayHomeAsUpEnabled(boolean)
Then you have to overwrite the method onOptionsItemSelected(MenuItem) and look for the android.R.id.home event to come in. Then you know the user has clicked on that back button on the actionbar
回答12:
You need to add the below mentioned code in the manifest file. Search for the activity in which you want to add the back arrow functionality. If you find the one then fine or create the activity
<activity android:name=".SearchActivity">
</activity>
Then add the following three lines of code in between.
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.raqib.instadate.MainActivity" />
And don't forget to add this piece of code in onCreate(); method of your particular activity in which you need back arrow.
Toolbar toolbar = (Toolbar) findViewById(R.id.searchToolbar);
setSupportActionBar(toolbar);
try{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}catch(NullPointerException e){
Log.e("SearchActivity Toolbar", "You have got a NULL POINTER EXCEPTION");
}
This is how i solved the problem. Thanks.
回答13:
First you need to write this codes
@Override
protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Then add this line in manifest
<activity android:name=".MainActivity"
android:parentActivityName=".PreviousActivity"></activity>
I think it will work
回答14:
You can also simply put onBackPressed()
in your onClick listener. This causes your button to act like the default "back/up" buttons in android apps!
回答15:
It can also be done without code by specifying a parent activity in app manifest If you want a back button in Activity B which will goto Activity A, just add Activity A as the parent of Activity B in the manifest.
回答16:
Toolbar toolbar=findViewById(R.id.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getSupportActionBar()==null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
回答17:
This is working for me.. Suppose there are two activity (Activityone,Activitytwo)
Inside Activitytwo use this code
@Override
protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
On Activityone
//when you need to go second activity
startActivity(new Intent(Activityone.this, Activitytwo.class));
This should be included in second activity inside manifest file
<activity android:name=".Activitytwo"
android:parentActivityName=".Activityone"></activity>
And The result would be like this
回答18:
This is working for me getSupportActionBar().setDisplayHomeAsUpEnabled(false); enter image description here