I've been working on an app with multiple ActionBar activities, and from the outset the HomeAsUp button has been visible and working on all of the activities selected from the MainActivity. Today I noticed that it is no longer there - but I can't figure out what I have changed that would affect it.
The most significant change in recent days is that I migrated the project from Eclipse to Android Studio because I was having problems with Eclipse frequently corrupting my project files and requiring a complete reinstall.
This is a sample of the code in the main activity which creates the new activities:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent=null;
int id = item.getItemId();
switch (id) {
case R.id.action_list:
intent = new Intent(this, StudyListActivity.class);
break;
// etc
}
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
Here is the declaration of the intent in the manifest
<activity
android:name="com.example.app.StudyListActivity"
android:label="#string/activity_list"
android:parentActivityName="com.example.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
</activity>
since it stopped working I have also tried putting
getSupportActionBar().setHomeButtonEnabled(true);
in the onCreate method of the called activity (as per Display back button on action bar) but that makes no difference.
So I'm wondering if there is anything that got stripped out in my migration from eclipse which is preventing it showing (or something else I could have changed which might inadvertently have disabled it)
you should enable it via setDisplayOptions
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
You could also try
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In the activity you want the HomeAsUp icon in
Related
For some reason this doesn't work in one of my activities but everywhere else it works fine when i press the back arrow in the toolbar it goes back to my MainActivity.
import androidx.appcompat.widget.Toolbar;
//Initializing toolbar
mToolbar = findViewById(R.id.mToolbar);
setSupportActionBar(mToolbar);
if((getSupportActionBar() != null)){
getSupportActionBar().setTitle(R.string.equalizerTitle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_home_up);
}
Manifest
<activity
android:name=".activities.EqualizerActivity"
android:parentActivityName=".activities.MainActivity"
android:screenOrientation="portrait"/>
EDIT
This activity uses a constraint layout as parent but i don't see why this would be the cause, all other activities use RelativeLayout and there they work fine.
Looks like you are using onOptionsItemSelected() method to handle menu buttons. The issue is that the "back" button is also kind of a menu button.
In order for Android to know that you don't have a specific handler for the button and it should be handled by the framework, you need to let Android know it via returning false when you don't "consume" the event. According to the docs:
onOptionsItemSelected()
boolean Return false to allow normal menu processing to proceed, true to consume it here.
So, you should have smth like this:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case YOUR_BUTTON:
// do something
return true;
}
// By default, allow Android to work on it
return false;
I am building a new application which composed of 3 activities namely:
- Splash Screen
- Activity A
- Activity B, and
- Activity C
From activity A user can go to both activities indicated below:
A -> B -> C (from act A user can go to B then to C).
A -> C (from act A user can go straight to C).
B -> C (from B user can go to C).
i also pass Serializable intent Extra between activities.
The problem that i am having is whenever i pressed the back button on the Action Bar (Top Left Corner) it always makes my app crashes (Error: NULL Pointer Exception).
I have tried to place this code on ALL of my activity.
#Override
public void onBackPressed() {
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
}
I tried to somehow mimic the physical backbutton behaviour since it is working when user press physical backbutton. But somewhat throws error as well.
or
public void onBackPressed(){
super.onBackPressed();
}
or (well this one literally restart the app, which is discourage since it restart the app from splash screen).
public void onBackPressed(){
super.onBackPressed();
finish();
}
Does anyone know the appropriate way to implement back button?
The button on the top left corner is not the "Back" button, it would be the "up" button, and it's just a button in the action bar, onBackPressed refers to the hardware back button being pressed.
Navigation with back and up is not necessarily the same ("back" means go to where I was before, whereas "up" means go to the upper level in the app hierarchy).
Take a look at http://developer.android.com/design/patterns/navigation.html for more information.
(Also, try to avoid a splash screen, they are highly discouraged in android design patterns)
Edit: I forgot to mention how to actually handle the "up" button. You do that on your activity's onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// Handle "up" button behavior here.
return true;
} else {
// handle other items here
}
// return true if you handled the button click, otherwise return false.
}
You can do something like this when the back button is called
public void onBackPressed(){
// Add data to your intent
finish();
startActivity(intent);
}
And similar in your onClick method for your action bar button.
I think, the problems is from passing value using intent.
If the value is intended to be use by all of your activity, I think the best way
is to use a sharedpreferences.
If You are using API level 16 or more than , Really No need to do anythinngggg,
i just Find the solution :
in manifest file with every other activity Except MainActivity Add meta-data like this :
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gsetia.ohama.MainActivity"/>
with in Activity like this , For Example
<activity
android:name=".ViewReports"
android:label="#string/title_activity_view_reports"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gsetia.ohama.MainActivity"/>
</activity>
And In Last , delete public boolean onOptionsItemSelected(MenuItem item) Method. That's it.
You will find your Back Button and will work fine
I'm just learning to work with Android through a textbook (Learn Android App Development by Jackson). At the moment, I have a MainActivity class. I'm adding Intents to the menu of this activity to launch one of four other activities depending on which option. All activities are in the same package, and all have been declared in the AndroidManifest.XML file.
I am determining which activity to run using a Switch case as follows:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
System.out.println("Add");
Intent intent_add = new Intent(this, NewPlanet.class);
this.startActivity(intent_add);
break;
case R.id.menu_attack:
System.out.println("Attack");
Intent intent_attack = new Intent(this, AttackActivity.class);
this.startActivity(intent_attack);
break;
case R.id.menu_config:
System.out.println("Config");
Intent intent_config = new Intent(this, ConfigActivity.class);
this.startActivity(intent_config);
break;
case R.id.menu_travel:
System.out.println("Travel");
Intent intent_travel = new Intent(this, TravelActivity.class);
this.startActivity(intent_travel);
break;
default:
return super.onOptionsItemSelected(item);
}
System.out.println("Outside switch.");
return true;
}
The problem, however, is that this only works when pressing the "Add" menu button, which successfully launches the NewPlanet activity and displays it.
All of the others, however, produce an ActivityNotFoundException and force the program to crash (same result on various combinations of virtual devices, as well as on my physical Galaxy Note II device).
I've done everything I can think of to try to fix this to no avail. As far as I know, the code is identical to that presented in the book, but the book has begun to move onto the next sections while my project is not yet working yet.
I have LogCat output if anybody wants to see that, but any help or advice would be greatly appreciated. I've Googled the issue which did not help much.
EDIT: As requested, here is my Manifest: (I attached it as a high-res images since I'm having trouble with the editor right now)
http://i.imgur.com/c7wJ8bM.png
And here is the relevant LogCat output:
07-03 10:42:53.954: E/AndroidRuntime(29754): FATAL EXCEPTION: main
07-03 10:42:53.954: E/AndroidRuntime(29754): android.content.ActivityNotFoundException: Unable to find explicit activity class {chapter.two.hello_world/chapter.two.hello_world.ConfigActivity}; have you declared this activity in your AndroidManifest.xml?
FINAL EDIT:
I've solved this problem thanks to user E. Odebugg: I was referring to my activities in the manifest by incorrect names (ConfigPlanet instead of ConfigActivity). I simply did not notice the difference. A silly mistake, but it has been fixed now. Thank you everybody for your help!
There might be something wrong with your manifest. Maybe you copied and pasted the same attributes for all Activities and hence have multiple Launcher IntentFilter Categories. Can you post both, the stack trace and the Manifest?
Clearly, the screen-shot you have shared shows that you have declared some ConfigPlanet activity, while in your switch-case u are calling ConfigActivity.
Replace .ConfigPlanet from your AndroidManifest.xml with .ConfigActivity, making sure that you do have a ConfigActivity.java file in your proper package.
You don't have the ConfigActivity declared in your manifest.
Somehow you have an entry there for ConfigPlanet which is not the same. So that's the cause.
Make sure that all your activities are declared in your AndroidManifest.xml.
<manifest ... >
<application ... >
<activity android:name="your.packagename.AttackActivity" />
<activity android:name="your.packagename.ConfigActivity" />
<activity android:name="your.packagename.TravelActivity" />
...
</application ... >
...
</manifest >
For details, see http://developer.android.com/guide/components/activities.html
In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
I have an issue with actionBar compatibility on API level 15 for Android. The up button doesn't work well with this API level
I use the sample project called "actionbarcompat" provided in the android-sdk folder, so I have imported all class's and I extends all my activities with ActionBarActivity. I also add this piece of code in the Manifest for all my activities :
<activity
android:name="fr.appsolute.rescue.MyActivity"
android:label="#string/title_activity_info"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
and in my ActionBarActivity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
This permit the user to touch the up button (with the app icon) to return to the MainActivity
This code works well with Android 2.3.3 and 4.2, but doesn't work with 4.0.1 and I don't understand why. When the user touch the up button, nothing happens.
Thanks for your help.
PS : I can't use an external library, I have to use native code
Not sure why it isn't working. I agree it is strange that it only fails on API 15.
An alternative to calling NavUtils.navigateUpFromSameTask(this) could be calling finish(). This would close your current activity and go to the previous activity in the stack. It may give you the same desired result.
Solved it using ActionBarSherlock