In my android app i am using up navigation icon on actionbar.
In my child activity i have set
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
In manifest I have set
<activity
android:name="app.sclms.UserAccount"
android:label="#string/app_name"
android:parentActivityName="app.sclms.MainMenu">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="app.sclms.MainMenu" />
</activity>
But whenever I click on up icon my application get exited.
What is missing here, I don't understand.
Please read this section: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp
Specifically, add this code:
#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);
}
I however do not recommend using navigateUpFromSameTask as it will cause your ParentActivity to be re-created instead of being resumed on ICS and lower. The reason for this behavior is because NavUtils behaves differently for Pre JellyBean and Post JellyBean as explained in this SO.
A better way is to do this is to handle the Up action manually in the Child Activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
startActivity(new Intent(this, ParentActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP));
default:
return super.onOptionsItemSelected(item);
}
}
And best part of this manual approach is that it works as expected(resumes the ParentActivity instead of ReCreating it) for all API Levels.
Related
I'm playing around with Android Studio so I created a SettingsActivity using the wizard and I'm faced with the problem that it is not possible to navigate from this settings activity back to the main activity using the "up" arrow in the Actionbar.
The setup of the Actionbar looks like this:
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Actionbar is not null btw.
And the parentActitvityName is set in the AndroidManifest:
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.demo.app.MainActivity" />
</activity>
However, a click on the arrow does nothing. Not even onOptionsItemSelected gets triggered.
Seems like this is exactly the same problem
Action bar setDisplayHomeAsUpEnabled not working on ICS but navigating back from a detail to an overview activity is working fine in the very same app. Moreover I set MinSDK to 15 and TargetSDK to 23.
override the onOptionsItemSelected method on your AppCompatPrefernceActivity and make it like this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Vspallas answer is correct. The mistake was on my side. I had an onOptionsItemSelected method inside the preferenceFragment, not in the Activity. Mea culpa.
You can use this inside the settings activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Then in androidManifest is the same as that you have did
<activity
android:name=".SettingsActivity"
android:parentActivityName=".MainActivity"
android:label="#string/title_activity_settings">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.MainActivity" />
</activity>
this com.example.app_name is you App package name
I want to just add back button in Right side of action bar and I found many link for this.
This is my code which is app->res->menu->main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/backAction"
android:icon="#drawable/ic_launcher"
android:title="back"
android:showAsAction="always"/>
</menu>
Optionmenuactivity.java
Oncreate event:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
onoptionItemselected()
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Manifest File :
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
Problem with this code:
Activity Flow is: (Main)Activity1-> Activity2 ->Activity3 ->Activity4
after Activity4, if we press backbutton, it should go on Activity3 but with this code it goes Mainactivity which is Activity1
Please help me ..Thank you
There are a lot of problems here.
Firstly I recommend you, to understand the difference between Back and Up
Up navigation explained with examples
Back navigation explained with examples
Secondly, it's not very easy to understand what's happening in your code, but as far as I see, you HAVE NOT declared in the manifest.xml the parent elements.
This is a nice and simple example:
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
A parent activity definition can EASE your life. This way you can define which activity will be loaded on UP NAVIGATION interaction.
Hope that helps :)
First of all i wanted to say what you are trying to do is not a good practice, why would you want a back button in your action bar if Android devices already have one on the bottom of the screen and you can turn on Up-Navigation at the left side of the Action Bar?
Custom App-Navigation clutters your code, confuses users and most of the time is just ugly.
But if you really need a button that does the navigation, you want it to access the standard android navigation methods.
For Back Navigation:
Just use the standard android back action onBackPressed().
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If this function doesn't do what you want, you can just override it with your own wanted behavior.
For Up Navigation:
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You will have to configure your parent activites in the manifest, but because of that the behavior of this Navigation is easier to change.
Pressing on the app's icon doesn't change the activity, but I'd like it to return to the app's MainActivity.
I've made my app's icon appear on the left of the action bar by doing the following:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
I'm assuming I should define some functionality in the onOptionsItemSelected() methods of the activities concerned. I've gotten a log of the MenuItem's ID when the app's icon is pressed and onOptionsItemSelected() gets called, but obviously I can't hardcode that id into the switch statement. What should I do?
You should set android:parentActivityName for your activity in the manifest file.
android:parentActivityName="com.example.myfirstapp.MainActivity"
You also need to handle the click in onOptionsItemSelected for the id of android.R.id.home:
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);
}
See more on Android developer website.
This is how I'd do it
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.HomeButtonId){
//Return Home
}
}
I have an Android app that uses the action bar, and the flow through the system is determined in the manifest as so:
<... android:parentActivityName="com.polymorph.amsmobile.xyzActivity" >
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="com.polymorph.amsmobile.xyzActivity" />
However there is a screen that users can go to from several points within the hierarchy and as such there is more than one 'parent' activity, so I want to dynamically specify the parent activity rather than specifying in the manifest.
My goal is to ensure that when they use the 'up' on the action bar, it will take them to the correct page of the app.
Do you mean something like this in an Activity from where you want to be able to go Up to another activitiy from the ActionBar?
#Override
protected void onCreate(Bundle b) {
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, GoToThisActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
}
If you just want to go to the previous activity, you can just do:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
break;
}
I am seeing the up caret but nothing happens when I click it. Where do i tell it to goto home and set which activity is Home?
You need the line:
getActionBar().setDisplayHomeAsUpEnabled(true);
put that in onCreate();
That will make it clickable. The you will need to handle the click event. This is done by overriding onOptionsItemSelected()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
If you want to use NavUtils, (which I have seen used by the ADT plugin when it makes Activities) you can replace change the implementation like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't forget to
import android.support.v4.app.NavUtils;
Also, you can add this to your Activity node:
<activity
android:name=".SomeActivity"
android:label="#string/activity_label" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.MainActivity" />
</activity>
NavUtils is usually the better approach. For more information, see the official Android Guidelines.
In your onCreate you have:
getActionBar().setDisplayHomeAsUpEnabled(true);
And then use:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Updated to address comments below, which correctly point out my original answer was not correct for all cases. The "home/up" button have are supposed to take you the Home Activity or the Parent Activity, respectively. From the ActionBar documents --
By default, your application icon appears in the action bar on the
left side. If you'd like, you can enable the icon to behave as an
action item. In response to user action on the icon, your application
should do one of two things:
Go to the application "home" activity, or
Navigate "up" the application's structural hierarchy
Implementing "Home" is straightforward. Launch an intent to the home activity (usually your main launch activity) of your app, clearing the backstack to the existing instance if one exists--
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
"up" is similar, except you should use
getActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate to get the icon to show as "up" instead of "home", and launch an instance of the parent activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In both case, for Activities that can be launched from outside your application, you should add FLAG_ACTIVITY_NEW_TASK to launch into a new task instead of the callers task.
"Up" can get more complicated if your Activity can be launched from another app, and want to build a back stack for your application to get back to the root. See for this guide for more info.
My original answer, shown below, is considered bad form in the guide because it it treats the home/up button as 'back'. It will take you to the activity that invoked this activity, which is not necessarily the home or parent. It works well as 'up' for apps that have strict tree hierarchy of activities because an given activity was always launched by its parent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}