I have many activities in my application
in that I have Given this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
to Go back to Home Page from other activities
like Below
If I press those three buttons all menus are working
this is my Option menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.abc) {
Intent mypIntent = new Intent(this, abc.class);
startActivity(mypIntent);
return true;
}
else if (id == R.id.web) {
Intent webIntent = new Intent(this, Web.class);
startActivity(webIntent);
return true;
}
else if (id == R.id.about) {
Intent aboutIntent = new Intent(this, About.class);
startActivity(aboutIntent);
return true;
}
.
.
.
..
return super.onOptionsItemSelected(item);
}
Here There is no menu named id == R.id.login or id == R.id.home but its going to login few days back its gone to home activity
but If I press Back.. action back is redirect to Login page Inst-ed of Home
I have added a Login page for my application using shared preferences.. and it is now launcher activity..
Here In my Login activity on if once user is sign in it should it should redirect to Home activity on every time..
and its working fine..
But on action bar when I press arrow button it is redirecting to empty login page..
if I press cancel entire app is working .. my credentials are safe except this action bar..
Update
I have Given Intent also if Login credentials success redirect to Home activity on app start up it will check every time
every thing is as for fine except action back
how to fix this...
Alright make sure u do declare activities as below -
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
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>
Now in every activity add below code block-
#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);
}
Update
add a New class file to check login or not else use home as default.. and replace your new class as launcher in Manifest
Just override this method.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
{
Intent intent = new Intent(mContext, Activity.class);/*your activity name*/
startActivity(intent);
}
default:
return super.onOptionsItemSelected(item);
}
}
Related
I'm currently developing an android application and i'm looking to make the logo on the left of the action bar a menu button. I have enabled the logo using the code
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
from my research I should be able to make this a a menu button by using the code
if (id == R.id.home) {
Intent i = new Intent(getApplicationContext(), MenuActivity.class);
startActivity(i);
}
in onOptionsItemSelected but this is not working for me.
Is there a way to change or find the correct ID for this logo as im starting to think R.id.home is incorrect
I think with enabling your home button with
actionBar.setDisplayHomeAsUpEnabled(true);
and then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//fire intent or whatever you require.. do over here
return true;
default:
return super.onOptionsItemSelected(item);
}}
I think this should work fine.
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;
}
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;
}
I am seeing the up caret but nothing happens when I click it. Where do i tell it to goto home and set which activity is Home?
You need the line:
getActionBar().setDisplayHomeAsUpEnabled(true);
put that in onCreate();
That will make it clickable. The you will need to handle the click event. This is done by overriding onOptionsItemSelected()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
If you want to use NavUtils, (which I have seen used by the ADT plugin when it makes Activities) you can replace change the implementation like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't forget to
import android.support.v4.app.NavUtils;
Also, you can add this to your Activity node:
<activity
android:name=".SomeActivity"
android:label="#string/activity_label" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.MainActivity" />
</activity>
NavUtils is usually the better approach. For more information, see the official Android Guidelines.
In your onCreate you have:
getActionBar().setDisplayHomeAsUpEnabled(true);
And then use:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Updated to address comments below, which correctly point out my original answer was not correct for all cases. The "home/up" button have are supposed to take you the Home Activity or the Parent Activity, respectively. From the ActionBar documents --
By default, your application icon appears in the action bar on the
left side. If you'd like, you can enable the icon to behave as an
action item. In response to user action on the icon, your application
should do one of two things:
Go to the application "home" activity, or
Navigate "up" the application's structural hierarchy
Implementing "Home" is straightforward. Launch an intent to the home activity (usually your main launch activity) of your app, clearing the backstack to the existing instance if one exists--
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
"up" is similar, except you should use
getActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate to get the icon to show as "up" instead of "home", and launch an instance of the parent activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In both case, for Activities that can be launched from outside your application, you should add FLAG_ACTIVITY_NEW_TASK to launch into a new task instead of the callers task.
"Up" can get more complicated if your Activity can be launched from another app, and want to build a back stack for your application to get back to the root. See for this guide for more info.
My original answer, shown below, is considered bad form in the guide because it it treats the home/up button as 'back'. It will take you to the activity that invoked this activity, which is not necessarily the home or parent. It works well as 'up' for apps that have strict tree hierarchy of activities because an given activity was always launched by its parent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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;
}