Navigate back from settings activity - android

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

Related

Android - back navigation from activity to previous one (no back arrow)

I have 2 activities: HomeActivity and EmailChangeActivity and some fragment inside HomeActivity. What I want to do is to set back navigation arrow in EmailChangeActivity toolbar. I have more activities and somehow I managed to make that arrow (but I made Intents straight from activities, not from fragment inside activities). What I did in order to get that arrow was:
When I go to EmailChangeActivity from HomeActivity I call this in HomeActivity fragment:
Intent intent = new Intent(getActivity().getApplicationContext(),
ChangeEmailActivity.class);
getActivity().startActivityForResult(intent, 1);
Inside EmailChangeActivity I insert standard code creating Toolbar and setting it:
// Setting Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getString(R.string.change_email_activity_name));
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
In Manifext.xml I made:
<!-- Home Activity -->
<activity
android:name="com.example.nazwamarki.myapplication.HomeActivity"
android:label="#string/home_activity_name"
android:theme="#style/AppTheme.Normal">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Change Email Activity -->
<activity
android:name="com.example.nazwamarki.myapplication.ChangeEmailActivity"
android:label="#string/register_activity_name"
android:theme="#style/AppTheme.Normal"
android:parentActivityName=".HomeActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.nazwamarki.myapplication.HomeActivity" />
</activity>
Still got no result. Any help?
Edit
I forgot to add that I also included in EmailChangeActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
If you're not using an ActionBarActivity:
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// The intent to go back to HomeActivity
}
});
If you are using an ActionBarActivity, then all you need to do is make Android use the Toolbar as an ActionBar.
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Looks like you're missing this part:
#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);
}
You can get more details on this here Android Developers- Providing up navigation

Back button is not working properly in Right side of action bar

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.

Back button works, but the actionbar button does not?

I have a pretty simple use case which seems to be working for all my Activities except one. All my Activities are calling getActionBar().setDisplayHomeAsUpEnabled(true);. The only place this isn't working is on an Activity I just added, which is also the only one I'm launching from the main activity's info menu. The actual OS back button works as expected in this case, but the Home button is not. Here's the menu logic that starts the activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.share:
share();
return true;
case R.id.support:
emailSupport();
return true;
case R.id.settings:
settings();
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The problem is with the SettingsActivity, called in the menu by the settings() method you see in the switch above:
private void settings(){
Intent intent = new Intent(InfoMenuActivity.this, SettingsActivity.class);
startActivity(intent);
}
And the onCreate of the SettingsActivity is pretty simple too:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = this.getSharedPreferences(getString(R.string.preference_file), Context.MODE_PRIVATE);
setView();
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
}
I originally only had the first getActionBar() call, but added the second to see if it changed anything, but it did not.
What am I missing?
From the comments it seems that you don't know how to set the parent activity in the manifest and keep supporting older versions.
For API 16 you can use parentActivityName attribute but for older versions you'll have to add a meta tag:
<activity
android:name="com.example.SettingsActivity"
android:label="Settings Activity"
android:parentActivityName="com.example.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
If all you want to do is override the up button functionality to act as a back button then don't forget that in every activity you'll have to override onOptionsItemSelected.
However remember that overriding any default and expected android behaviour is bad practice.

Up Navigation from Actionbar in Android

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.

Add back button to action bar

I have been trying to add a back button to the action bar.
I want my view to look like this:
I want to add the back button in the left of the action bar.
I added this code
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
but it doesn't work.
How can I fix this?
After setting
actionBar.setHomeButtonEnabled(true);
Add the following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Add
actionBar.setHomeButtonEnabled(true);
and then add the following
#Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.
this one worked for me:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ... other stuff
}
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.
After setting
actionBar.setHomeButtonEnabled(true);
You have to configure the parent activity in your AndroidManifest.xml
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" />
<activity
android:name="com.example.SecondActivity"
android:theme="#style/Theme.AppCompat" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html
There are two ways to approach this.
Option 1: Update the Android Manifest
If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar
<activity
android:name=".SettingsActivity"
android:label="Setting Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.example.MainActivity" />
</activity>
Option 2: Change a setting for the ActionBar
If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your #imports match the level of compatibility you are looking for).
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_test);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
Then, detect the 'back button' press and tell Android to close the currently open Activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That should do it!
Firstly Use this:
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
Then set operation of button click in onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method
Duplicate of Android Sherlock ActionBar Up button
Simpler and better:
For API >= 16
Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
Use this to show back button and move to previous activity,
final ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back_dark);
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
if anyone else need the solution
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
Add this line in onCreate() method
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
then Override this method
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}

Categories

Resources