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.
Related
When I use an intent to move from parent activity to another, and then use action bar nav to go back to parent, the parent activity has lost all my changes and values (as a user) and is back to default xml display i have for it.
I have these in my Child activity OnCreate:
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
And labeled my parent activity appropiately:
android:parentActivityName=".MainActivity"
What do I need to make it store or remember those changes when traveling between activities?
In the Manifest file, you should set android:launchMode like this:
<!-- ACTIVITY -->
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:screenOrientation="portrait"/>
P.s: read Launch Mode on this link:
https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
There are couple of options to try -
1) start the new activity with startActivityForResult()
2) in the manifest file, set the android:launchMode="singleTop" of the parent activity
Hope it helps
You might be creating a new instance of your parent when you click the Navigation button. A simple approach is to just finish your activity so whatever activity in the backstack will simply be resumed.
Something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
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,
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;
}
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.
I`m using SherlockActionBar for my application. In my manifest I define icon for logo and launch.
android:icon="#drawable/ic_launcher"
How to make it clickable and handle the event of it`s pressing? I want to be able to back to my dashboard by pressing the logo.
Use
actionBar.setDisplayHomeAsUpEnabled(true);
with
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can read more about the home button here http://developer.android.com/guide/topics/ui/actionbar.html#Home
It explains how to clear previous the stack of previous activities and has some more links to best practices on navigation using the home button.