Up Navigation is not working - android

So I have a very simple project. It contains two activities. The main activity has a navigation drawer and a fragment container. The second activity is merely meant to display details when the user interacts with a certain fragment.
So I have set my main activity as the parent activity to my second activity (called DetailsPage) like this:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailsPage"
android:label="#string/title_activity_details_page"
android:parentActivityName="com.collusion.serviceassistant.MainActivity" >>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.collusion.srviceassistant.MainActivity"/>
</activity>
and in the DetailsPage activity code I have the following:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.share :
share();
return true;
case R.id.home:
Log.i("BACK", "Going back!");
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new
// task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
Now it seems that by my log, the code that pertains to the up action is not getting executed. Whenever I hit the up caret in the action bar or use the back hardware key, the app simply exits. Does anyone have any idea what I am doing wrong here? I am wondering if it has anything to do with the fact that the details page activity does not extend the ActionBar class:
public class DetailsPage extends Activity{
Does anyone have any ideas?

According to Google docs link on navigation using the home button you might want to use
android.R.id.home instead of R.id.home
#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 think this might solve the home up navigation.

Related

Why does up navigation close the app instead of directing to parent activity even though parent activity is specified in manifest.xml?

I have set up my manifest xml to indicate the parent activity, yet the navigation up button in the action bar simply exits the app instead of directing to the specified parent activity.
I have already tried using meta-data tag, still the same result.
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
<activity
android:name=".RegisterActivity"
android:label="#string/title_activity_register"
android:parentActivityName=".LoginActivity">
There is nothing special about LoginActivity and RegisterActivity.The RegisterActivity is started by the LoginActivity using intents.
I had the same problem too, override onOptionsItemSelected to solve that
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//return to your parent activity
return true;
}
}

Navigate up not working

I have three activities A->B->C. Sometimes I launch Activity C directly from A. Even then, when the user presses the "up" button, I want to go to activity B. So after a lot of searching, I am doing this:
AndroidManifest.xml
<activity
android:name=".B"
android:parentActivityName=".A"
android:launchMode="singleTop"/>
<activity
android:name=".C"
android:parentActivityName=".B"/>
In Activity C:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this,upIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But when I start activity C from A, the up button just goes back to A, instead of creating a new instance of B.
Even the android docs state:
When you call this method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward.
Am I missing something here?

Activity Up-Navigation don't work

I have an Activity which should be able to navigate up to its parent. This is how I declared it in the Manifest:
<activity
android:name="com.myapp.SeminarActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:theme="#style/Theme.AppCompat.Light"
android:parentActivityName="com.myapp.SeminarOverviewActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.myapp.SeminarOverviewActivity" />
</activity>
It works when I enter SeminarActivity via an Intent in SeminarOverviewActivity. But it is possible to enter SeminarActivity from an other Activity. This activity has noHistory=truebecause I do not want to go back to it via Up-Navigation. When I enter SeminarActivity form this Activity and Press back, the App ends. I also tried use NavUtils and added this from the android documentation:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
}
return super.onOptionsItemSelected(item);
}
but no effect. In my tests, always the else part of the above snipped is used.

Preserve state between activities

I have two different activities, ActA which is my main Activity, and ActB. I get to ActB from ActA through a menu voice. If I press the back button (either software or hardware) in ActB the app come back to ActA preserving my previous state in this activity. If instead I press the up button in the activity bar, the ActA reset its state. How can I prevent this to happen?
Don't know if this could ever help, but this is some code:
Activity A
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_save:
startActivity (new Intent (this, Save.class));
return true;
case R.id.action_about:
}
return false;
}
Manifest for Activity B:
<activity
android:name="com.myapplication2.app.Save"
android:label="#string/title_activity_save"
android:parentActivityName="com.myapplication2.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.myapplication2.app.MainActivity" />
</activity>
Add this switch case:
switch (item.getItemId()) {
case android.R.id.home: finish(); return true;
}
The way it works, Up (android.R.id.home) by default calls your Activity with the same Intent, and depending on how you handled onCreate and onResume it may be the case that you're recreating your views. If you change Up behaviour to match Back the state is preserved.
You won't need android:parentActivityName or meta-data, just add
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
somewhere on your lifecycle code.

How can I return to a parent activity correctly?

I have 2 activities (A and B) in my android application and I use an intent to get from activity A to activity B. The use of parent_activity is enabled:
<activity
android:name=".B"
android:label="B" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.A" />
</activity>
I also use a theme which provides an UP-button.
So after I called activity B I can use the UP-button to get back to the activity A. The problem is that the application seems to call the onCreate()-function of activity A again and this is not the behaviour I need. I need activity A to look the same way like it looked before I called activity B.
Is there a way to achieve this?
EDIT
I didn't write any code to start activity B from activity A. I think it is auto-generated by Eclipse.
Class B looks like:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_b, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
You declared activity A with the standard launchMode in the Android manifest. According to the documentation, that means the following:
The system always creates a new instance of the activity in the target
task and routes the intent to it.
Therefore, the system is forced to recreate activity A (i.e. calling onCreate) even if the task stack is handled correctly.
To fix this problem you need to change the manifest, adding the following attribute to the A activity declaration:
android:launchMode="singleTop"
Note: calling finish() (as suggested as solution before) works only when you are completely sure that the activity B instance you are terminating lives on top of an instance of activity A. In more complex workflows (for instance, launching activity B from a notification) this might not be the case and you have to correctly launch activity A from B.
Updated Answer: Up Navigation Design
You have to declare which activity is the appropriate parent for each activity. Doing so allows the system to facilitate navigation patterns such as Up because the system can determine the logical parent activity from the manifest file.
So for that you have to declare your parent Activity in tag Activity with attribute
android:parentActivityName
Like,
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.app_name.A" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name=".B"
android:label="B"
android:parentActivityName="com.example.app_name.A" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.A" />
</activity>
With the parent activity declared this way, you can navigate Up to the appropriate parent like below,
#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);
}
So When you call NavUtils.navigateUpFromSameTask(this); this method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward as defined by FLAG_ACTIVITY_CLEAR_TOP.
And to display Up button you have to declare setDisplayHomeAsUpEnabled():
#Override
public void onCreate(Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Old Answer: (Without Up Navigation, default Back Navigation)
It happen only if you are starting Activity A again from Activity B.
Using startActivity().
Instead of this from Activity A start Activity B using startActivityForResult() and override onActivtyResult() in Activity A.
Now in Activity B just call finish() on button Up. So now you directed to Activity A's onActivityResult() without creating of Activity A again..
I had pretty much the same setup leading to the same unwanted behaviour. For me this worked:
adding the following attribute to an activity A in the Manifest.xml of my app:
android:launchMode="singleTask"
See this article for more explanation.
Although an old question, here is another (imho the cleanest and best) solution as all the previous answeres didn't work for me since I deeplinked Activity B from a Widget.
public void navigateUp() {
final Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {
Log.v(logTag, "Recreate back stack");
TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
} else {
NavUtils.navigateUpTo(this, upIntent);
}
}
[https://stackoverflow.com/a/31350642/570168 ]
But also see: https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android
A better way to achieve this is by using two things:
call:
NavUtils.navigateUpFromSameTask(this);
Now, in order for this to work, you need to have your manifest file state that activity A
has a parent activity B. The parent activity doesn't need anything. In version 4 and above you will get a nice back arrow with no additional effort (this can be done on lower versions as well with a little code, I'll put it below)
You can set this data in the manifest->application tab in the GUI (scroll down to the parent activity name, and put it by hand)
Support node:
if you wish to support version below version 4, you need to include metadata as well.
right click on the activity, add->meta data, name =android.support.PARENT_ACTIVITY and value = your.full.activity.name
to get the nice arrow in lower versions as well:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
please note you will need support library version 7 to get this all working, but it is well worth it!
What worked for me was adding:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
finish();
}
to TheRelevantActivity.java and now it is working as expected
and yeah don't forget to add:
getSupportActionbar.setDisplayHomeAsUpEnabled(true); in onCreate() method
I tried android:launchMode="singleTask", but it didn't help.
Worked for me using android:launchMode="singleInstance"
Adding to #LorenCK's answer, change
NavUtils.navigateUpFromSameTask(this);
to the code below if your activity can be initiated from another activity and this can become part of task started by some other app
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(upIntent)
.startActivities();
} else {
NavUtils.navigateUpTo(this, upIntent);
}
This will start a new task and start your Activity's parent Activity which you can define in Manifest like below of Min SDK version <= 15
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.A" />
Or using parentActivityName if its > 15
I had a similar problem using android 5.0 with a bad parent activity name
<activity
android:name=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
I removed the com.example.myfirstapp from the parent activity name and it worked properly
In Java class :-
toolbar = (Toolbar) findViewById(R.id.apptool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Snapdeal");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In Manifest :-
<activity
android:name=".SubActivity"
android:label="#string/title_activity_sub"
android:theme="#style/AppTheme" >
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"></meta-data>
</activity>
It will help you
Add to your activity manifest information with attribute
android:launchMode="singleTask"
is working well for me
#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);
like a Back press
try this:
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
intent = getIntent();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_b, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpTo(this,intent);
return true;
}
return super.onOptionsItemSelected(item);
}
Going into my manifest and adding android:launchMode="singleTop" to the activity did the trick for me.
This specifically solved my issue because I didn't want Android to create a new instance of the previous activity after hitting the Up button in the toolbar - I instead wanted to use the existing instance of the prior activity when I went up the navigation hierarchy.
Reference: android:launchMode
Try this solution use NavUtils.navigateUpFromSameTask(this); in the child activity:
https://stackoverflow.com/a/49980835/7308789
In any context, If you want's to open parent activity on onBackPressed
protected fun navigateToParent() {
NavUtils.getParentActivityIntent(this)?.let { upIntent ->
if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot) {
Timber.d("navigateToParent: sourceActivity should recreate")
startActivity(upIntent)
} else {
Timber.d("navigateToParent: sourceActivity in stack, just finish")
}
}
super.onBackPressed()
}
Set android:parentActivityName in manifest
<activity
android:name=".feature.signup.SignUpActivity"
android:parentActivityName=".feature.welcome.WelcomeActivity"
android:windowSoftInputMode="adjustResize" />

Categories

Resources