Android Navigate To Previous Activity Not Working? - android

I have added a Toolbar to my AboutApplication activity. I have also added a navigation button to this toolbar -
when this button is clicked, the application should navigate to the previous activity (LoginActivity).
I tried calling the finish inside method onBackPressed() and this did not work. I tried creating an OnClickEventListener() for the toolbar's setNavigationOnClickListener() - this did not work either.
Here is the code for my AboutApplication activity:
public class AboutApplicationActivity extends AppCompatActivity {
private NavigationOnClickListener myListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_application);
myListener = new NavigationOnClickListener(this);
myListener.setNextActivity(new LoginActivity());
TextView toolbarTitle = (TextView) findViewById(R.id.aboutApplication);
Toolbar toolbar = (Toolbar) findViewById(R.id.aboutApplicationToolbar);
toolbar.setNavigationIcon(R.drawable.back);
toolbar.setNavigationOnClickListener(myListener);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

Do not confuse what you want with Navigation Design, you are requirement matches a lot like Up navigation provided by android, why do you want to make it around when this feature is already provided to you.
if your AboutApplication Activity comes from LoginActivity then you should apply Up Navigation provided my Android which know the best, let it handle it.
Things that you need to take care of while you are providing up navigation is that you properly define the child-parent structure in the AndroidManifest.xml file in your android project
<!-- Assuming this activity would be a parent -->
<activity
android:name="com.example.myfirstapp.LoginActivity" ...>
...
</activity>
<!-- this would be your child activity in your case `AboutApplicationActivity` -->
<activity
android:name=".AboutApplicationActivity"
>
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.LoginActivity" />
</activity>
now in your child activity things you need to make sure is this,
getActionBar().setDisplayHomeAsUpEnabled(true); // without support library
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // support library
and offcours by now you know where does this goes
#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);
}
if you have hard time importing NavUtils.java, than write its fully qualified name android.support.v4.app.NavUtils
for more details use android documentation here,

Related

Animation on Android Toolbar Back Button

I have an activity wich has a Toolbar which displays a back button.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:title="#string/app_name"
/>
The back button is enabled like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_about);
setSupportActionBar(toolbar);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
I call this activity from my main activity like this:
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
The Activity's parent is defined in the manifest
<activity android:name=".AboutActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".EntryActivity" />
</activity>
So far everything works fine, except that the transition animation is wrong when using the back button in the Toolbar.
When I open the activity, it slides in from the right.
When I press the phone's physical back button it slides out to the right again. This is correct.
However when using the Toolbar back button it slides out to the left. This looks wrong. How can I change this, so it duplicates the behaviour of the physical back button?
When you press the Actionbar Up button, AppCompatActivity detects this button press in its onMenuItemSelected() call, and invokes onSupportNavigateUp(). This method determines the "parent activity" Intent and uses that to navigate up. Because it's using an Intent to (re-)open the previous activity, it uses the same animation it would for opening a "new" screen.
Assuming you don't care about the particular niceties of the "Up Navigation" pattern (which it sounds like you do not, as comments have led me to believe you don't have lateral navigation and you can't get to your second activity from anywhere other than your first activity), you can side-step all of this built-in "up" behavior by overriding the onSupportNavigateUp() method.
#Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
This will mean that pressing the Actionbar Up button always simply finish()es your activity, so (like I said before) you lose out on all the smart built-in "up" behavior... but you didn't want that anyway.
You could also handle the Actionbar Up button in onOptionsItemSelected(), but I prefer the other way since I think it's a little more obvious that you're hijacking the system's up behavior.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
With either of these in place, you can remove the "parent" definitions from your manifest, since now they're not used.
Try this:
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
This is because the default launchMode of activities is standard.
The documentation of standard states the following:
The system always creates a new instance of the activity in the target task and routes the intent to it.
You can solve this by using android:launchMode="singleTop" on the parent/starting activity.
If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
For more information see Tasks and the back stack and android:launchMode.

Android: I can't seem to get my custom toolbar's back arrow to act like the device's physical back arrow

I have the back arrow implimented and working but instead of just finishing the activityB and going back to the previous view (fragment) the user was on in activityA. It instead closes activityB and reloads activityA which means you have to go back through a few menus to get back to where you were.
The physical back arrow does exactly as I want it to in that it just closes the activtyB and puts the user back to where they were within activtyA originally before they moved to activtyB.
This is my current code for the toolbar back arrow but maybe I'm just not fully grasping how the physical back arrow works. I appreciate any and all advice!
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
For every activity you have to add parent activity in Android manifest file.
<activity
android:name="com.example.myfirstapp.ActivityC"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
If you specified MainActivity as parent to ActivityC then it will go to MainActivity. If you specified ActivityB it will move to ActivityB on click on back button in ActionBar.
For more see at here
You need to override the onOptionsItemSelected method of your activity:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
onBackPressed();
} else {
super.onOptionsItemSelected(menuItem);
}
return true;
}

How to return one activity back with actionbarActivity back button?

I made a app with multiple activities. And there are some problem that I cannot solve on my own.
For example, I have 4 activities named "MainMenu","Secondpage","Thirdpage", and a "Fourthpage". From The MainMenu , I pass a id with putExtras() to pass over to the Secondpage so that the Secondpage gets the id and sets it into a url to get the json data from the web. After the "Secondpage" gets the json data,it will display it in a listView. The user will now be able to click the listView to mover over to the "Thirdpage" that contains the id and the data passed from the "Secondpage". Now the problem is that from this "Thirdpage", I want to go back to the "Secondpage" but I cannot find a way to do this.
I've set the manifest file like the follwing, however, I'm not sure if I'm doing it right. Or should I set something up in the ActionbarActivity class?
Sample
<activity
android:name=".Secondpage"
android:label="#string/title_activity_first_time_user"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Thirdpage"
android:label="#string/title"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.me.Secondpage" />
When you say
I want to go back to the "Secondpage"
you mean like if you have pressed the back button?
If so, you can just call finish(); in your third activity to do that.
EDIT:
In your third Activity, add
getActionBar().setDisplayHomeAsUpEnabled(true);
or, if you are using the support library
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate(Bundle b) method
And
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
elsewhere in your class.
Just call
onBackPressed()
if you want to have a behaviour like the user uses his back button. You should return to the SecondPage without any problems.
EDIT:
If you want to use the back button in the action bar check this one out.

Action bar setDisplayHomeAsUpEnabled closes app

My app uses the android.support.v7.app.ActionBar so that I can use the Action Bar on my Android 2.3.4 phone and my tablet using Android 4.4.2. I've got my minimum and target SDK's set at 10 and 18. When I click the back button on the Action Bar which calls setDisplayHomeAsUpEnabled(true) it works correctly on the 2.3.4 phone and goes back to the Home Activity called Main_Interface. But when I click the back button on the Action Bar on the tablet, it closes the app as if I clicked the back button on the tablet. Nothing comes up in LogCat, so it's acting as though it's supposed to close the app when the Action Bar setDisplayHomeAsUpEnabled() button is clicked. Here's what the Activity looks like in the Manifest:
<activity
android:name=".UILNPPager"
android:configChanges="orientation|screenSize"
android:label="UILNPPager"
android:parentActivityName="com.myapp.ktg.Main_Interface"
android:theme="#style/Theme.Blu_uilpager" >
<intent-filter>
<action android:name="com.myapp.ktg.UILNPPAGER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.myapp.ktg.Main_Interface" />
</activity>
Here's the Activity stripped of everything but the ActionBar calls:
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class UILNPPager extends ActionBarActivity {
private static final String STATE_POSITION = "STATE_POSITION";
DisplayImageOptions options;
ViewPager pager;
String[] imTitlesStr;
String[] imMediumStr;
String[] imPriceStr;
String[] imageUrls;
int pagerPosition;
PhotoViewAttacher mAttacher;
ActionBar mActionbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbar_uilpager);
//--- ViewPager stuff
....
//--- END ViewPager stuff
/** Getting a reference to action bar of this activity */
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, 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);
}
}
I tried taking the call android:parentActivityName="com.myapp.ktg.Main_Interface" out of the manifest, but that has no effect. It still works correctly on the 2.3.4 phone and not on the 4.4.2 tablet.
Any idea why it doesn't work right on the tablet?
Please use a AppCompat theme or a theme that inherits one of them. For example:
#style/ThemeAppCompat
#style/ThemeAppCompat.Light
#style/ThemeAppCompat.Light.DarkActionBar
I noticed that you are using your own styled theme, you can inherit the theme from one of the AppCompat theme as follow.
<style name="Theme.Blu_uilpager"
parent="#style/ThemeAppCompat.Light">
....
</style>
Quoted from a official Android tutorial, https://developer.android.com/training/basics/actionbar/styling.html
Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher). In doing so, each style property that you declare must be declared twice: once using the platform's style properties (the android: properties) and once using the style properties included in the Support Library (the appcompat.R.attr properties—the context for these properties is actually your app). See the examples below for details.
In my app I'm using my own transitions to move from Activity A to Activity B. I've discovered that the call overridePendingTransition(); in the onPause(); call in Activity A breaks the getSupportActionBar().setDisplayHomeAsUpEnabled(true); call in Activity B, which has the ActionBar. So on Activity A, I removed the onPause() method and called overridePendingTransition(); in the Intent that calls Activity B, like this:
Intent miI = new Intent();
miI.setClass(Splash.this, Main_Interface.class);
startActivity(miI);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
Then in Activity B I removed the onPause(); method and added overridePendingTransition(R.anim.fadein, R.anim.fadeout); to the onCreate(); method like so:
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.mainpage);
Then I put the overridePendingTransition(); call in all other Activities' onCreate(); methods just like in Activity B. Doing this makes the transition work correctly. And to make the ActionBar go back to the Main_Interface Activity correctly on my 4.4.2 tablet, I just call getSupportActionBar().setDisplayHomeAsUpEnabled(true);. And everything is working correctly!

How to handle "Up" button?

How to handle "Up" button (SDK version 11+)? I am referring to the one at the top of screen, that holds the application icon.
In Android Design articles it was named as "Up button", but I didn't found it (or similar) in KeyEvent fields.
Implement onOptionsItemSelected() and watch for android.R.id.home "menu" events, as is described in the documentation.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// do something useful
return(true);
}
return(super.onOptionsItemSelected(item));
}
First change your AndroidManifest.xml file to have a parent activity declared. Eg
<activity android:name=".theory"
android:parentActivityName=".MainActivity"
android:label="#string/theory"
/>
<activity android:name=".experimental"
android:parentActivityName=".MainActivity"
android:label="#string/exp"
/>
Do this for all the activities other than the MainActivity.
Note the parentActivityName xml code
Then go to the respective java files and add the following code
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
You have you up button enabled now.

Categories

Resources