0

I wonder what kind of approach available for the transfer of the variable generated from class of fragment or popup window to activity provided that the fragment or popup window class is separated from activity.

Any code example of elegant approach is appreciated.

Anndexi9
  • 141
  • 2
  • 15

3 Answers3

1

It all depend of what kind of data you wish to pass between fragment or popup window to activity one way can be using intent

    //create an Intent object 
        Intent intent=new Intent(context, Activity.class);
    //add data to the Intent object
        intent.putExtra("text", "Data");
    //start the second activity
        startActivity(intent);

and for receiving intent data use

getIntent().getStringExtra("text")

Another way can be using sharedpreferences

SharedPreferences prefs = this.getSharedPreferences(
      "com.example.app", Context.MODE_PRIVATE);

To read preferences: String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()

long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

To edit and save preferences

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).apply();
Moien.Dev
  • 1,030
  • 2
  • 10
  • 18
1
  1. In the fragment, create an interface (let's call it VariableCallback for now) with one method of return type void that takes in a parameter with the same type as the variable you're generating. Let's call the method onVariableGenerated.
  2. Make the activity hosting the fragment implement that interface. Create a field in the fragment of type VariableCallback. Let's call it callback.
  3. Override the fragment's onAttach(Context context) method, and set the field to point to the context. Make sure you cast the context to VariableCallback.
  4. Now, when the fragment generates the variable, you can call callback.onVariableGenerated(myVariable), and that will pass the variable to the activity that hosts the fragment.
  5. Make sure you override the fragment's onDetach() method to set the callback field to null. This will prevent a memory leak of the activity.
prfarlow
  • 3,624
  • 2
  • 15
  • 24
  • What about the case then information in the activity is needed by the poupwindow or fragment? Can ((Activity) getActivity ()).functionCall (); work here? – kilokahn Mar 04 '18 at 10:36
0

Answering a little late but there's one more way you could do this that i could think of: Local Broadcasts

You could use a LocalBroadcast Manager and a BroadcastListener in the activity, and send a LocalBroadcast from the popupwindow:

In the main activity you could do:

LocalBroadcastManager localBroadcastManager = 
    LocalBroadcastManager.getInstance (getApplicationContext ());

BroadcastReceiver popupdatareceiver = new BroadcastReceiver () {
    @Override
    public void onReceive(Context context, Intent intent) {
        ...
        // code to handle received data goes here
        }
    }
};

localBroadcastManager.registerReceiver (popupdatareceiver, new IntentFilter ("popupdata"));

From the PopupWindow you could send a Local broadcast like so:

Intent popupdataIntent = new Intent ("popupdata");
Bundle popupdataBundle = new Bundle ();
...
// now add your data to the Bundle here
...
popupdataIntent.putExtra ("popupdata", popupdataBundle);

To send the data to the Activity, you'd need to initialize a LocalBroadcastManager instance and fire off a broadcast - this could be triggered by a Button's OnClickListener, or by the OnDismissListener of the PopupWindow

LocalBroadcastManager newLocalBroadcastManager = 
     LocalBroadcastManager.getInstance (getApplicationContext ());
newLocalBroadcastManager.sendBroadcast (popupdataIntent);
kilokahn
  • 1,136
  • 2
  • 18
  • 38