简体   繁体   中英

Can I move objects between activities without intents in Android?

The point is that from Activity A to BI need to create Activity B every time, but I don't close Activity A because when I return to Activity A from B, I simply finish Activity B but I don't create Activity A because I didn't finished it. That is what I want.

The problem is when I try to pass data and objects from Activity B to A. I can't use intents and putExtra() because I don't start Activity A, I simply resume it with onResume().

SharedPreferences don't let me to pass objects so does anyone knows if there's any method to pass objects from B to A?

SOLUTION: http://www.javatpoint.com/android-startactivityforresult-example

Thanks to @brightstar @ThMBc @Avtar Guleira @Edy Bolos that's was I looking for :)

You can start activity B using startActivityForResult, and then return from it to A your data inside intent. You can find example on it here:

http://developer.android.com/reference/android/app/Activity.html

There are different methods for different cases. If the object created in B is going to be used application wide, you might consider giving it to the appliction, so that every activity can reach it by

.getAppliction().getMyField() 

Note that using a static field in a general class almost comes down to the same thing, but this follows the rules of encapsulation.

If B is launched purely for the creation of the resulting object then starting an activity for result is the way to go, as brightstar said:

.startActivityForResult()

as documented in the android dev docs

Technically you can also pass objects through sharedPrefs if you serialize them (eg converting them to a json object and passing a string in prefs), but that is not really the way one would do this.

Yes you can do it. For Example: Create a standalone java class

Class C {
  public static ArrayList<YourObject> myList;

}

Import the C class header

enter code here
Class B {
   myList.add()
}

import C class header

Class A{
 myList.get(index)

}

Use an Singleton-Class to store and retrieve objects.

public class Model {
    private static Model model = null;

    private Object myObject;

    private Model() {}

    public static Model getInstance() {
        if (model == null) {
            model = new Model();
        }
        return model;
    }

    public Object getMyObject(){
        return myObject;
    }

    public setMyObject(Object myObject){
        this.myObject = myObject;
    }
}

When you are in Activity A you store the objects you need in the model and retrieve them afterwards in activity B

You can create another Class and create static properties which will hold your objects for you. Please look.

Class Utils {
  public static MyObject object;
  public static int index;
}

Use it like

Utils.object = new MyObject();
Utils.index = 4;

You cann't transfer object as per you scenario because you are not calling Activity A directly(means startactivity).You can achieve this scenario in another way. You have to set/save data in Application class from Activity B and then you can get data from application class in activiy A onResume() method and do whatever you want.

Follow below steps:

  1. Just start Activity B using startActivityForResult method instead of startActivity
  2. Then override onActivityResult in your Activity A.
  3. When you want to finish or onBackPressed Activity B. just pass data using intent in setResult Method

For complete documentation you can view this link: Getting a Result from an Activity

So, when ever Activity B will finished OnActivityResult will be called of Activity A. In this method you will get Intent that you have set from Activity B. You can whatever you wan to do man then.

Please just don't use static fields!!! It really bad practice, and you can encounter concurrency issues. You can easily accomplish your request by starting activity B with startActivityForResult, and then in activity B you can catch the result by overriding onActivityResult method, like described here: http://developer.android.com/reference/android/app/Activity.html#StartingActivities

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM