I have the following question: when I return from one activity to another, I need certain element of menu to be opened. For example:
Now, when I click "Up" in my activity the first element of menu is always opened (where the running man is drawed). And I need to make such element opened, which was opened before opening new activity. For example I choose an article in Articles menu element and after clicking Up in new activity I want to be opened Articles element again. I tried to realize it as clicking on Back button:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But it didn't help. What's the matter?
I think you have to keep the selected item and after returning manually select it.
See the example here: https://stackoverflow.com/a/43117486/7469860
In your Manifest file, search for your Activity node and add the following line:
android:launchMode="singleTask"
This will define how your activity will be launched.
Here is an official documentation about launchMode property https://developer.android.com/guide/topics/manifest/activity-element#lmode
Related
I am having problems getting my in-app menu navigation to work. Unfortunately, I have not been able to find a solution here so far. Since I'm quite a newbie to Android development, I'm not sure what info you guys need for better assistance, so bear with me.... ;]
Problem:
The "back to main menu" submenu option triggers the menu.performIdentifierAction(R.id.back_to_main_menu, 0) action. This works fine in principle, but only once. So, suppose the user navigates from the main menu to submenu A, he can return to the main menu and also go to submenu A again. But if he now wants to return to the main menu for the second time, the entire menu will be completely closed. Then the user can still click on all items, but the "Return to main menu" option does not work and instead simply closes the entire menu.
All other methods/actions associated with the menu items will be invoked properly.
What I tried so far:
I tried closing and inflating the menu again. But it lead to different other problems mostely due to not being compatible with MaterialToolbar and using an optionsMenu and related functionalities instead.
Also the solutions I found for selecting a specific menu element programmatically were about using the above mentioned method "menu.performIdentifierAction()".
App structure:
My app consists of three activities and one background service. In all activities I use the same xml menu. And the behavior is the same in all activities. The in-app menu consists of several items and submenus. In each submenu, I added a "back option" so that the user can return to the main menu without closing and reopening the menu.
The menu is included in the activity xmls for each activity, like (simplified):
\<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout_mainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"\>
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/materialToolbar_mainActivity"
style="#style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/material_toolbar_settings"
app:title="#string/page_title_mainActivity_text" />
</com.google.android.material.appbar.AppBarLayout>
Referenced like:
materialToolbar_mainActivity = findViewById(R.id.materialToolbar_mainActivity);
Clicked item handling like (simplified):
materialToolbar_mainActivity.setOnMenuItemClickListener(menuItem -\> {
// handle item selection
switch (menuItem.getItemId()) {
// ...
case (R.id.item_mainMenu_app_info):
showInfo();
return true;
// ...
case (R.id.item_submenu_backToMainMenu):
materialToolbar.getMenu().performIdentifierAction(R.id.item_main_menu, 0);
return true;
// ...
default:
return false;
}
});
If you have any suggestions or solutions, please don't hesitate and let me know. If you need any further information, please let me know as well.
With kind regards
My action bar has this button on the top right :
And when I click it, it gives me a settings menu item which does nothing on click:
I would like to disable the button in the first picture (looks like an ellipsis but rotated) from my ActionBar.
How would I go about doing that?
This is the default overflow menu that is set by the SDK when you create a new Android project.
To disable the menu you just have to remove the code that adds it. You should have a method onCreateOptionsMenu() overridden somewhere in your Activity, which you can remove.
selects a contextual menu item
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
more info here - http://developer.android.com/guide/topics/ui/menus.html#context-menu
I am a newcomer to android and am using Eclipse to learn to write action bars.
Code for res/menu/main:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
Code for MainActivity.java:
public boolean OnOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, there is a bug regarding R.id.action_search. It is said that it cannot be resolved or is not a field. How can I solve it?
One more question, am I going to define openSearch() and openSettings by myself or they are defined?
Thank you very much.
I believe that your project have to be built automatically to have the R.id working properly.
For your second question, if you want to open the system settings you should look here: How to open Settings of Android Phone on a button click in our Android App
For the search, I don't know if you're looking for a search bar or opening Google search, but for a search bar you should look at the section Add an action view on this page: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
I want to add a a menu item to the top (Where the class title is). I added the item to menu.xml and also for made an Intent to open a new class after clicking but it does not open my whole app anymore. And when I remove android:onClick:"history" it starts working again with out the menu function of course.
Have a look at the picture please: http://snag.gy/udxXc.jpg
That 'History' is my button that needs to open a new class. and this is my code:
xml:
<item
android:id="#+id/history"
android:showAsAction="always"
android:title="History"
android:onClick="history"/>
Java:
public void history(View view){
Intent historyIntent = new Intent(this, History.class);
startActivity(historyIntent);
}
Am i doing it correctly? or any better solution?
I want to add a a menu item to the top (Where the class title is)
That is called the action bar.
And when I remove android:onClick:"history" it starts working again with out the menu function of course.
I am not aware that android:onClick is valid for <item> elements.
Am i doing it correctly?
Not as far as I am aware.
or any better solution?
Override onOptionsItemSelected() and respond to the item click there.
I love this site all you guys are awesome! but here is another problem I have:
In my app I have a webview that displays a website in the entire screen, I have made a code to show a menu through pushing the phone's menu button from where i want 2 things to happen 1st menu item Go back to main screen of the app, 2nd menu item quit the app or exit the app.
First problem:
after pressing the menu button it shows the menu... if I press it again it shows the two choices twice, if I press it again now both items shows 3 times and so on!
Second Problem:
after choosing any of the two choices nothing happens!
here is my code please tell me what I'm doing wrong!
Thanks
menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menuToMenu"
android:title="Menu Principal"
/>
<item
android:id="#+id/menuToSalir"
android:title="Salir"
/>
</menu>
Backtomain.java
import android.app.Activity;
import android.os.Bundle;
public class Backtomain extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and where I call the menu:
public boolean onPrepareOptionsMenu (Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mostrar = getMenuInflater();
mostrar.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuToMenu:
startActivity (new Intent("my.app.BACKTOMAIN"));
return true;
case R.id.menuToSalir:
finish();
System.exit(0);
return true;
}
return false;
}
You are calling super.onCreateOptionsMenu() from onPrepareOptionsMenu(). And, you are inflating the same options into the menu in onPrepareOptionsMenu(). Rename onPrepareOptionsMenu() to onCreateOptionsMenu(), and it will probably behave better.
Also:
If you think the my.app.BACKTOMAIN activity is running, you probably want to add FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_CLEAR_TOP to the Intent.
Get rid of the menuToSalir menu choice. No well-written Android application will call System.exit(0). Users leave your application by pressing the HOME button, no different than they might in a Web app.
To fix your first problem try onCreateOptionsMenu(), rather than onPrepareOptionsMenu().
I am not positive how to fix second problem, I usually create my menus all in java instead of using xml like you have.
I found the problem it works! I was missing the "s" at...
public boolean onOptionItemSelected(MenuItem item){
the right way is
public boolean onOptionsItemSelected(MenuItem item){
thanks for your help Tim and CommonsWare
I ran into this problem. In my case I had set the background color of the activity screen to black. When the menu popped up it had a transparent background and black text so I didn't see it working.