how to use xml with intents? - android

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".

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 call (and use) onOptionsItemSelected from MainActivity in other classes

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.

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 to go back from second screen to first screen

How to switch layouts? First, I have a class Main where is onCreate (setContentView(R.layout.main);) and then I call, another class with command:
setContentView(secondClass);
In this class, I draw with Canvas and this work just fine. I also create button to go back in first "class" (R.layout.main), but I don't know how to do it.
Now my program is basic a graph shower. In first class you type your function and them second class draw it. But how to go back in first class to type another function. This "back" button or arrow witch every Android phone have, send me out of program not back on insert part.
In secondClass I can't create onCreate method, but I also tried the following and they didn't work:
Intent abc = new Intent("bla.bla.bla.FIRSTCLASS");
startActivity(abc);
and
Intent abc = new Intent(SecondClass.this,FirstClass.class);
startActivity(greNaPrvoOkno);
If you want to use a custom view (as I understood, you are extending the View class), you can do it in the following way;
Consider you are showing the second class from your Main activity like this;
setContentView(new SecondClass(getApplicationContext(), MainActivity.this));
And you Second class is this (suppose);
// I am using onClickListener to go back to main view. You do whatever you like.
public class SecondClass extends View implements OnClickListener {
// This is needed to switch back to the parent activity
private Activity mParentActivity = null;
public SecondClass(Context context, Activity parentActivity) {
super(context);
mParentActivity = parentActivity;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Set the Main view back here.
mParentActivity.setContentView(R.layout.main);
}
}
Disclaimer: This code will do what you have asked for, but may cause other problems.
As advised by #Mudassir, you should use two different activities for two screens. It will give you better control, and your code will be easy to understand and maintain.
On the Onclick event of the button you have to write finish(); that's it..
Both of your classes are Activities yes? IF so then in your second activity you will simply call finish() and your activity will close revealing your first activity again.
When I have used multiple intents in my android application, I have created a new activity through:
Intent abc = new Intent(this, SecondClass.class);
startActivity(abc);
When the button is pressed in your second class, I would then either call finish(); on the class, or create a new intent like so:
Intent abc = new Intent(this, FirstClass.class);
startActivity(abc);
However, this method has the disadvantage that if a user wanted to use the back button, they may have to scroll through many layers of activities.
You should create another activity for your second class but not just set the main activity to a new view setContentView(secondClass).
For an easier modification, You could try to set the view back to setContentView(R.layout.main) first.
You still need to configure the widgets(e.g. TextView) on it when you set it back.
You don't have to startActivity again to go back.
Just call finish() in your second activity when you want to finish the current activity and go back:
e.g. When user press the back button in your second activity
mButtonBack.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
}

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