Android: Start activity from a MenuItem
I'm new on Android, and I'm trying to start an Activity from a MenuItem
choose of the user.
Actually, I'm building my menu (and is working OK) from my main activity class using a MenuInflater
:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
//the Menu Inflater class allows to create a menu from a XML File
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.layout.menutest,menu);
return true;
}
And im handling the Menu selection using the following code (working fine too):
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.MenuItemNewWebsite:
ShowScreenAddSite();
break;
default:
break;
}
return false;
}
I have a second and last activity called AddWebsite, and i would like to start it, but the following code doesnt work:
protected void ShowScreenAddSite()
{
Intent i = new Intent(AddWebsite.class);
startActivity(i);
}
Do you know what is the extra thing that I have to pass to the Intent
constructor?
Asked by: Kevin151 | Posted: 25-01-2022
Answer 1
the solution was too simple, appears that in android, every activity class is not automatically referenced in the manifest.xml.
I just only add the new activity to the manifest, and works fine.
Regards. Jose
Answered by: Michelle116 | Posted: 26-02-2022Answer 2
I'm still quite new to android myself but don't you need to be passing a context to the Intent constructor?
protected void ShowScreenAddSite()
{
Intent i = new Intent(this, AddWebsite.class);
startActivity(i);
}
You're probably doing this from inside an activity so I think you should be using 'this'
Answered by: Kellan887 | Posted: 26-02-2022Answer 3
You can also do something like this
/* (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = new MenuInflater(this);
final Intent[] menuIntents = new Intent[] {
new Intent(this, AddWebsite.class) };
inflater.inflate(R.menu.mymenu, menu);
final int ms = menu.size();
for (int i=0; i < ms; i++) {
menu.getItem(i).setIntent(menuIntents[i]);
}
return super.onCreateOptionsMenu(menu);
}
avoiding some method calls, however you need to pay attention to the mapping between menu ids, menu order and intents but this is almost always known.
Answered by: Alberta229 | Posted: 26-02-2022Similar questions
Android: Force dialog themed activity to be modal
A button on our screen causes an activity to be shown that has a "dialog" theme. We are having an issue were if you click the button quickly twice in a row, the dialog activity is opened twice.
Typically I would expect that when a new activity is started, the underlying activity is immediately stopped, and thus accepts no further input.
However, since the dialog themed activity does not take over the who...
Android: What happens to my activity in case of incoming phone call?
I want to know that what will happen to my activity incase of an incoming phone call? Will the state of my activity be saved or I have to explicitly save it?
Another question is that will the activity resume after the call is disconnected?
Please help..!
Android: How can I start an activity from a regular java class? the java class is servlet
How can I start an activity from a servlet? I have an i-Jetty server installed and a servlet that responds to HTTP request. I would like to start an activity from the servlet, does any body have any idea on how to do this?
Android: Adding data to Intent fails to load Activity
I have a widget that supposed to call an Activity of the main app when the user clicks on widget body. My setup works for a single widget instance but for a second instance of the same widget the PendingIntent gets reused and as result the vital information that I'm sending as extra gets overwritten for the 1st instance. So I figured that I should pass widget ID as Intent data however as soon as I add In...
Android: view/ drawable custom styles possible?
What I'd like to do is change the state (really, the background) of an EditText to reflect validity of its contents. E.g. if the user enters 999 where 999 is contextually invalid, the EditText should have a red border in place of the default orange border, likewise once the text is valid it should have a green border.
Methods I've explored:
Changing the style of the EditText programmatically via some...
Android: Crop an Image after Taking it With Camera with a Fixed Aspect Ratio
I'm trying to crop an image after taking it, and my code is as follows:
private void doTakePhotoAction() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
...
Android: Keep GPS active
i'm having a main activity that runs a gps listener.
If i'm starting a new activity the listener is destroyed.
I need the gps to be still activated, also after starting the 2nd activity.
Do i have to implement an own listener there, or is there another solution?
Android: is it possible to add ZIP file as a raw resource and read it with ZipFile?
Is it possible to add ZIP file to APK package as a raw resource and read it with ZipFile class? It looks like it's trivial to open file from SD card, but not from APK.
adb - Android: Delete entire database
I would like to delete a complete database created by my application.
Do you know any adb command, or android sentence to do it?
Android: Internal Linkify does not work properly, and text, images flicker inside ListView
I enabled the Linkify property of a textview as follows:
txtbox.setAutoLinkMask(Linkify.WEB_URLS);
But, when there are URLs like bit.ly are present (which is very common nowadays in messages like tweets), it doesn't display them properly. The problem is "sometimes" it succeeds and sometimes it fails... The other problem is, if this TextView is part of a custom view for a ListView, then unt...
Android: textView doesn't show cursor
I have a textView which is configured as an EditText. But the problem is that the cursor doesn't appear when i'm pressing keys (text is written correctly).
Thanks
Android: Flush DNS
Closed. This question is off-topic. It is not curre...
Android: Handling Activity stack
I've got the following:
Activities A, B, C, D. A and D can be reached at any time, anywhere from the application.
B and C are reached like this:
A -> B -> C
I have the following use case:
The user has entered C ( A -> B -> C ) then she has gone to D. ...
Android: Move whole layout upwards
I have a LinearLayout, when user selects my AutoCompleteTextView(ACTW) I want to move the whole layout upwards, so that the ACTW is at the top and there is space between the ACTW and software keyboard for suggestions. 1) How to do this? 2) How to make this animated (but this is not necessary)?
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android