How to call (and use) onOptionsItemSelected from MainActivity in other classes - android

I'm making my very first Android application but I ran into a problem.
I have over 8 different classes which all use the same actionbar.
Now in place of calling the method in every different class (and having a lot of double code) I would like to call the method of the main class in my other classes.
This is a part of my code for the onOptionsItemSelected in main.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.actionbar_new_income:
Intent newIncome = new Intent(this, NewIncome.class);
this.startActivity(newIncome);
return true;
}
}
Now I was wondering how I could call the method in another class (newIncome.java)
I have this so far, but it keeps saying I need to add arguments. And I ofcourse need to be able to detect which menuitem is clicked..
MainActivity main = new MainActivity();
main.onOptionsItemSelected();
Any help please?
Thanks!

You should not do this. If you have common code then put it in a class (not an activity) that is accessible by any activity that needs it.
You will still have some duplication but this is normal.
A good way of reducing activity launch code is to add a static method to each activity that you can call which launches the activity it is in.
E.g in your NewIncome Activity you could have
Public static void Launch(Context c) {
Intent newIncome = new Intent(c, NewIncome.class);
C.startActivity(newIncome);
}
You could then launch this activity from any other activity just by calling
NewIncome.Launch(this);
If required you can add parameters to the method and then add Extras to the Activity using these parameters.

You can do it like the following example if your menu entries are totally independent of the activity in which they are contained:
In each activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return CommonClass.HandleMenu(this, item.getItemId());
}
In a common class
public class CommonClass {
public boolean HandleMenu (Context c, int MenuEntry) {
switch (MenuEntry) {
case R.id.actionbar_new_income:
NewIncome.Launch(c);
etc....
...
}
}

If your 8 classes are activities you may define a base activity with the onOptionsItemSelected which is the one where you put the elements in the actionbar you want. Then make the other activities derive from it.

Related

How can i intent this code to another activity?

How can I pass this code to my another activity, I created two activities one is home activity and another is video_activity I call this code in video_activity in bottom nav but I also want to call it on my home activity I tried too much by I am not getting the correct one and please help me ... I want to pass this code from video activity to home activity ...
case R.id.navigation_dashboard:
favourite = toolbar;
favourite.setTitle(item.getTitle().toString());
videos.clear();
fetch_db("favorit");
favouritList();
return true;
You can wrap it in a function and call it from other activities
Example:
public class A {
public void foo(){ //your code ... }
}
In other activities:
A namefunction = new A();
namefunction.foo();
Or you can use the static method
Here are several instructions you may want to look: link 1 , link 2

How to pass interface to constructor when creating an Intent

I have two Activities: the MainActivity starts the NewReminderActivity. The first one will be notified when a new reminder has been created. Therefore it implements the interface OnEventAddedListener.
Do I need to use serialization to add the MainActivity to the intent or is there a better solution? I've never seen any examples using serialization to accomplish this and I'm sure it's very common to pass an interface from one activity to another in order to communicate.
public class MainActivity extends Activity implements OnEventAddedListener {
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.action_addReminder)
{
// NewReminderActivity c = new NewReminderActivity(this);
// Intent intent = new Intent(this, c.getClass()); // this won't work
Intent intent = new Intent(this, NewReminderActivity.class);
startActivity(intent);
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
}
You absolutely should not try to pass one activity to another, whether it's by serializing it (which won't even work for a number of reasons) or setting a reference.
Android will take care of cleaning up old activities out of memory, but won't be able to do so as long as you're holding on to a reference from it. Never hold on to other activities or fragments outside of their context!
You should follow the documentation on starting activities and getting results by using startActivityForResult() and provide that activity's result through onActivityResult(int, int, Intent).

How can I call android Activity without startAcitivity(intent)?

In the main page of my android app I've added OptionsMenu.
What i want is when the option "ContactUs" is selected a new Activity called "ContactUs" should get called.
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getTitle().toString().equals("ContactUs")) {
ContactUs contact = new ContactUs(Home.this);
}
return true;
}
And when the ContactUs activity is opened its layout should be displayed:
public class ContactUs extends Activity{
private Context CONTEXT;
public CustomEqualizer(Context c){
this.CONTEXT = c;
setContentView(R.layout.contactus);
}
}
But the problem is the layout will not be displayed until onCreate method is called.
My question is how can I call ContactUs option from Home without startActivity?
startActivity() is the only way to start an Activity. The problem you're having is that you're trying to call setContentView() in the constructor which won't work anyway since it hasn't gotten the application context yet. You override the onCreate() method and call it from there.
-= update =-
You can pass whatever you want between activities by adding it to the Intent's bundle.

how to use xml with intents?

I'm writing an application which I want to be able to launch a second class with a different layout when a menu button is pressed. the code I have to switch classes is:
case Menubutton1:
Intent i = new Intent(Budgeter.this, Outgoings.class);
startActivity(i);
return true;
(Obviously within a case statement)
How do I create an xml file which only relates to the second class? Also do I need to edit AndroidManifest.xml?
Finally if anyone could point me towards some good tutorials on intents I would greatly appreciate it.
in first class write a method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem mnuHome =menu.add(0, 0, 0, "Home");
mnuHome.setAlphabeticShortcut('h');
mnuHome.setIcon(R.drawable.home_icon);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle() == "Home") {
Intent i = new Intent(Budgeter.this, Outgoings.class);
startActivity(i);
finish();
}
}
And in second class you have to override onCreate() method and there you can set Layout as
setContentView(R.Layout.XML);
Also for second class you have to define in menifest.xml
I'm guessing here that you're confused about thinking the layout file for an Activity has to be called main.xml??? If so, this is not the case...
You can have budgeter.xml, outgoings.xml etc etc.
Just use setContentView(R.layout.budgeter) in the Budgeter Activity's onCreate(...) method and setContentView(R.layout.outgoings) in the Outgoings Activity onCreate(...) and so on.
Also do I need to edit AndroidManifest.xml?
Yes, all Activities must be registered in AndroidManifest.xml
As for working with Intents, try this as a starter...
Intents and Intent Filters
you should add the second(any) class in AndroidManifest.xml file if the class extended from android core components(Activity,Service,ContentProvider,BroadcastReceiver,BroadcastReceiver is a little different).It is possible to create another XML file in "layout" under "res".

Creating a public intent in Android

So I was trying to make a menu in an application that has a single item. Clicking this menu item takes you to another activity. So basically I'm starting a sub-activity on having that menu item clicked.
The problem is here that I have to specify my intent in the onCreate() method of my main activity, but I need this intent in my onOptionItemSelected() method which is outside the onCreate() method. How do i do this?
My onCreate() method:
public void onCreate(Bundle savedInstanceState) {
Intent myIntent=new Intent(mainActivity.this,secondActivityt.class);
}
My onOptionsItemSelected() method:
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==R.id.aboutbtn){
startActivity(myIntent);
}
So here I'm unable to access myIntent. Is there anyway I can declare the intent outside the onCreate method and still make the program work, or anyway I can access the intent outside the onCreate method?
should be pretty easy, just declare a private class variable and store your intent in that like
public class MyActivity extends Activity{
private Intent myIntent;
public void onCreate(...){
...
myIntent = new Intent(...);
}
public boolean onOptionsItemSelected(MenuItem item){
...
//use the myIntent here
}
}
I didn't try it out now, but I don't see any reason why this should not work, unless I didn't clearly understand your issue :)
Just as a sidenote. You maybe should also give a look at the Activity lifecycle, basically which event (onCreate, onResume, onStart, onDestroy,...) is fired when. The following figure taken from Android Developers describes it best:

Categories

Resources