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).
Related
I am wondering about the best way to design / program this:
If I have a Boolean value (let's say whether the user has extra power or not) and I need to pass it from Activity A to B to C. Should I add it to an intent from each activity to another OR should I just store it in a static variable and access it every time?
Its is safer to pass it in the intent. sometimes android kills apps without warning when it needs memory and your static values will not be retained on the other hand intent extras are kept. if you want to push it a little further, use shared preference. its designed using Map data struct so speed will not be a problem.
Android Intents have a putExtra method that you can use to pass variables from one activity to another.
public class ActivityA extends Activity {
...
private void startActivityB() {
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("HAS EXTRA POWER", false);
startActivity(intent);
}
}
public class ActivityB exntends Activity {
Bundle bundle;
private void hasExtraPower() {
bundle = getIntent().getExtras();
if(!bundle.getBoolean("HAS EXTRA POWER")
// do something
else
// do something else
}
}
Passing data through Intent
If you use that only in that activity that's fine but
When u need to pass to other layer like viewmodel that will make your operation's speed slower
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.
I want to manage all activities with class conductor like this:
Also all activities extend base activity to use common view.
In this case, I want to handle transfer activity, for example:
Base -> First -> Second -> Third -> First
Base -> First -> Fourth -> Fifth -> Fourth
When transferring activity, Conductor must handle all activity in stack.
I try to write this conductor as below (I use list to manage instead of stack):
public class Conductor {
private List<Activity> listOfActivityInStack;
public Conductor(){
listOfActivityInStack = new ArrayList<Activity>();
}
public void startActivity(Activity activity, Class<?> cls){
listOfActivityInStack.add(activity);
Intent i = new Intent(activity.getApplicationContext(), cls);
activity.startActivity(i);
}
public void startActivityForResult(Activity activity, Class<?> cls, int requestCode){
listOfActivityInStack.add(activity);
Intent i = new Intent(activity.getApplicationContext(), cls);
activity.startActivityForResult(i, requestCode);
}
public void startAcitivtyClearPrevious(Activity activity, Class<?> cls){
listOfActivityInStack.clear();
listOfActivityInStack.add(activity);
Intent i = new Intent(activity.getApplicationContext(), cls);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
}
public int getCount(){
if(listOfActivityInStack == null)
return 0;
return listOfActivityInStack.size();
}
}
I store this conductor in Global variable. Then I use it as below:
//Get conductor from application global
conductor.startActivity(FirstActivity.this, SecondActivity.class);
//Then add conductor to application global
But I have some problem:
I must handle goBack() for all activity to remove activity from list.
Check activity has exist in list, if yes, try get its instance.
Is there best way to manage all activity on android? I have tried search but not found good answer. I wonder weather or not my way is right. Any recommend or example would be help!
there is one way to do this. you must save your all activities state. and when you need to recall them you must use that state.
for extra info look here:
Saving Android Activity state using Save Instance State
I am new to android so please excuse the newbie question. I have a game I am trying to port from an old Java applet to android. My goal is to get this functional and then post an article on a site like CodeProject (or a better one if there are ones more appropriate). The idea is to show that a person brand new to android development can create an app in a reasonable amount of time.
I am making some progress but have run into a problem. I have the main activity in which the user interacts with. I then created a menu item that in turn starts a second activity (call it child) with a modest number of checkbox's, seekbar's etc to fill in parameters. I can successfully pass the class containing all the options from main to child. But I cannot get the child to pass this data back to the main.
First here is my main code that starts the child activity:
public void addBalls()
{
Intent myIntent = new Intent(this, GameOptions.class);
Bundle b = new Bundle();
b.putSerializable("options", gameParams);
myIntent.putExtras(b);
startActivityForResult(myIntent,STATIC_OPTIONS_VALUE);
}
The data passed to the child (and hopefully back again) is:
public class GameOptionParams implements Serializable
{
private static final long serialVersionUID = 1L;
public int speedBarPosition;
public int vgravityBarPosition;
public int mgravityBarPosition;
public int viscosityBarPosition;
public int restititionBarPosition;
public boolean trace;
public boolean collide;
public boolean mush;
public boolean wrap;
public boolean flicker;
}
And here is the expected return (again in main)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case (STATIC_OPTIONS_VALUE) :
{
if (resultCode == Activity.RESULT_OK)
{
//retrieve intended options
Bundle b = data.getExtras();
gameParams = (GameOptionParams) b.getSerializable("options");
}
break;
}
}
}
The child activity successfully receives the gameParams data. It then interacts with the user to update the values and then I attempt to return it but it does not seem to get sent to main. Here is the child code in the onStop() override.
Maybe this code should not be in the onStop() override but I can't determine where else to place it.
#Override
public void onStop()
{
super.onStop();
//read widget values
gameParams.speedBarPosition = speedBar.GetPosition();
gameParams.vgravityBarPosition = vgravityBar.GetPosition();
gameParams.mgravityBarPosition = mgravityBar.GetPosition();
gameParams.viscosityBarPosition = viscosityBar.GetPosition();
gameParams.restititionBarPosition = restititionBar.GetPosition();
//todo save to persistent
Intent resultIntent = new Intent(this, TiltBall2ImpactActivity.class);
Bundle b = new Bundle();
b.putSerializable("options", gameParams);
resultIntent.putExtras(b);
setResult(Activity.RESULT_OK, resultIntent);
}
Back in the main onActivityResult override I always see requestCode=0, resultCode=0, data=null. I assume this is a typical newbie problem, I have been reading the sdk documentation, user forums etc and have come close to a solution but just not quite there yet. Any help would be appreciated.
Since this is sort of a setting menu for the game, I assume you are going to need these values for more than one activity. If so you extend the android.app.Application class.
In that class you can create attributes to hold your values. In any activity you can call
MyApplication myApp = (MyApplication)getApplicationContext();
where myApp is a singleton. So you will get the values you set from another activity.
You will need to add this code to your application tag in the manifest file for it to work
android:name=".MyApplication"
If you need to keep these values for next startup of the application, you need to use SharedPreferences. This is a good tutorial for that
http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html
Assuming in your 'child' activity, the user has to press an 'OK' or 'Save' button then put the code to set the gameParams parameters in the button's onClick(...) handler.
Use the default constructor for instantiating the Intent, example...
Intent resultIntent = new Intent();
...then after creating the Bundle and adding gameParams to it and calling setResult(...), simply call finish() to terminate the 'child' activity. There aren't many occasions that I can think of to override onStop() and I suspect you don't want to be using it to attempt returning the Intent.
Suppose I have a class first.java (activity class) and I start another activity in this class (second.java - activity class).
How can I access the instance of first.java from second.java?
Can someone give me a good explanation on this... An example would be great...
If you need your second activity to return some data to your first activity I recommend you use startActivityForResult() to start your second activity. Then in onResult() in your first activity you can do the work needed.
In First.java where you start Second.java:
Intent intent = new Intent(this, Second.class);
int requestCode = 1; // Or some number you choose
startActivityForResult(intent, requestCode);
The result method:
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// Collect data from the intent and use it
String value = data.getString("someValue");
}
In Second.java:
Intent intent = new Intent();
intent.putExtra("someValue", "data");
setResult(RESULT_OK, intent);
finish();
If you do not wish to wait for the Second activity to end before you do some work in the First activity, you could instead send a broadcast which the First activity reacts to.
You can simply call getParent() from the child activity.
I have no clue why other answers are so complicated.
Only this should work
class first
{
public static first instance;
oncreate()
{
instance = this;
}
}
first.instance is the required thing that is accessible from the second class
try this if this work 4 u.........
something like this.....
class first
{
public static first instance;
oncreate()
{
instance=this;
}
public static getInstance()
{
return instance;
}
}
now from second class call first.getInstance();
you can also directly acess instance in static way like this first.instance.......
Thanks...
You can't create an activity directly.
In the first activity take a static activity variable like this,
public static Activity activity;
In the onCreate do this.
activity = this;
Then in the second activity do this,
Activity activity = (your activity name).activity;
Edit:
For passing data from one activity to other activity this is not the way.
Above answer was to get activity instance from other activity which was initially asked.
To pass data from one activity to other activty generally use bundle. But if the data is not primitive data type, then use object class which should implement parcelable or serializable interface. Then through bundle only parcelable list of objects we can pass.