Dynamic parent activity for Android up \ back navigation in Preferences menu - android

I've a Preference activity for a options menu in my Android app.
I've enabled the Up \ Back navigation on the ActionBar and I need to come back to the previous activity that called the Options menu.
For the Preference activity, I could use in the manifest:
android:parentActivityName="mypackage.com.MainActivity"
but how come back to other activies ? The Options menu is called from 4 different activities.
public class Prefs extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
}
}

Make sure that you have declared the parent activity in the manifest like this...
<activity
android:name="com.myapp.SetPreferenceActivity"
android:parentActivityName="com.myapp.MainActivity"
>
and then make sure to add the case into your onOptionsItemSelected method...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Take me back to the main activity
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
if you prefer to have the up button point to a custom activity, you can just use an intent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent changeActivity = new Intent(this,OtherActivity.class);
startActivity(changeActivity);
return true;
}
return super.onOptionsItemSelected(item);
}

The default behaviour of the Back button is that it will get you back to the calling activity. The system maintains a back stack of activities while the user navigates the application. Do you need to override this functionality? Please be more specific.

Solved in this way:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
}
return true;
}

Related

How to go back from 3rd activity to second activity by back button?

I just started android programming and I created 3 activities.A button in each activity will open another.I can navigate from my 2nd activity back to my first but I can't navigate from my third activity back to my second.Here's what I tried but the app crashes when I click on the back button. I guess it's trying to jump from the third activity to the second. If it helps my second activity's java file is called CountryDetails.java
public class WikiPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
getSupportActionBar().setTitle(R.string.title_activity_wikipedia_details);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return 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);
}
}
Simply call onBackPressed() instead of NavUtils.navigateUpFromSameTask(this)
Remove NavUtils.navigateUpFromSameTask(this);
You should use onBackPressed() only.
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.
case android.R.id.home:
onBackPressed();
return true;
Use onBackPressed() instead of NavUtils.navigateUpFromSameTask(this) for activity backstake manage

Back button as Menu in fragment

I want to add a back button as menu in the left of the action bar in fragment. But I don't want the back arrow as my icon.
actionBar.setDisplayHomeAsUpEnabled(true);
The above code line gives the back arrow symbol. Instead i want to use some custom image. Also using that custom image should get it back to its previous activity.
I added something like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It worked for me.
Add this to your theme in styles.xml:
<item name="android:homeAsUpIndicator">#drawable/my_icon</item>
It will override the default icon. By default it uses the following id:
android.R.id.home
You can use this id to navigate back to your previous activity:
View homeView = getActionBar().getCustomView().findViewById(android.R.id.home);
homeView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Go back
}
});
or
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Return back to your activity!
return true;
}
return super.onOptionsItemSelected(item);
}
For more information check the official documentation at : https://developer.android.com/training/implementing-navigation/ancestral.html

Up Button in ActionBar with the possibility to move more activities

I'm doing an app for Android. In the DetailActivity, there is the ActionBar with the Up Button. When I press it, it would return in the previous activity. But the DetailActivity can be launched from different activity.
Instead of using NavUtils.navigateUpTo(this, upIntent) , you can just use finish() or onBackPressed(). This would finish the activity, and go back to the previous one. So handling of the Up button being pressed would look 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:
finish();
return true;
}
return super.onOptionsItemSelected(item);

Using actionbar home as up button, home activity saveInstanceState is always null

Activity A ===click button===> Activity B
When press back button, Activity A is not recreated.
When press home as up button, Activity A is recreated.
So I save state when A.onSaveInstanceState(Bundle outState)
, and use state when A.onRestoreInstanceState(Bundle savedInstanceState).
Saving and Using works fine (except home as up button)
.
However,
When pressed home as up button,
system recreate Activity A, and savedInstanceState is gone.
How can I use Saved Instance State?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// I do not want this...
// Home as up button is to navigate to Home-Activity not previous acitivity
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
In the onCreate() enable the home button.
#Override
public void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
In the onOptionItemSelected() method do this.
#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);
}
This should enable Up navigation. If you want the parent activity to be restored with a savedInstanceState. You should set launchMode="singleTop" in the parent activity in Manifest file.
For more info check out http://developer.android.com/: Providing Up Navigation
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I used finish() insteed of NavUtils;
As #Joachim mentioned in a comment, there is a difference between R.id.home and android.R.id.home. In my case, I have been using R.id.home, which did not work, and android.R.id.home did work.

Android action bar 'up' items, with activities that have more than one parent

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;
}

Categories

Resources