Return value for another action

app design

I have an application that basically looks like the design in the attached picture. As you can see, there are 4 activities and some of them have fragments.

I want to return the test response to the user profile. So far I've been uploading the result to the server and updating the user profile every time I go back to ProfileActivity , but that seems like a waste of resources.

Is there a sample or a way to do this in the application? I looked at this : it seems doable if I can somehow link the two startActivityForResult()

s.

I have considered using the delegate pattern described in this question , but I cannot get the UserFragment instance for TestActivity .

I hope someone can point me in the direction of the correct way to do this.

+3


source to share


2 answers


One approach is to use startActivityForResult()

does not endActivity

start all actions startActivityForResult()

, then depending on your state, you can end the activity.

returns the result to the previous activity with onActivityResult()



Then for the fragment, you can save the fragment object in ProfileActivity . In the snippet write method to update the UI

So you can access this method using the fragment object

class ProfileActivity extends ......
{
  @override
  public void onActivityResult(....)
  {
    .....
    frgamnetObject.updateUI();
    ....
  }
}

      

+1


source


Assuming you are getting one int value from the last page:

create class:

public class GenericUtility {

public static int getIntFromSharedPrefsForKey(String key, Context context)
{
    int selectedValue = 0;

    SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE);
    selectedValue = prefs.getInt(key, 0);

    return selectedValue;
}

public static boolean setIntToSharedPrefsForKey(String key, int value, Context context)
{
    boolean savedSuccessfully = false;

    SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();

    try
    {
        editor.putInt(key, value);
        editor.apply();
        savedSuccessfully = true;
    }
    catch (Exception e)
    {
        savedSuccessfully = false;
    }

    return savedSuccessfully;
}

public static String getStringFromSharedPrefsForKey(String key, Context context)
{
    String selectedValue = "";

    SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE);
    selectedValue = prefs.getString(key, "");

    return selectedValue;
}

public static boolean setStringToSharedPrefsForKey(String key, String value, Context context)
{
    boolean savedSuccessfully = false;

    SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();

    try
    {
        editor.putString(key, value);
        editor.apply();
        savedSuccessfully = true;
    }
    catch (Exception e)
    {
        savedSuccessfully = false;
    }

    return savedSuccessfully;
}
}

      

than setting an int value for it:

GenericUtility.setIntToSharedPrefsForKey("selected_theme", 1, getApplicationContext());

      

or



GenericUtility.setIntToSharedPrefsForKey("selected_theme", 1, MyActivity.this))

      

and in the very first activity where you want the result:

int selectedValue = GenericUtility.getIntFromSharedPrefsForKey("selected_theme", getApplicationContext());

      

or



int selectedValue = GenericUtility.getIntFromSharedPrefsForKey("selected_theme", MyActivity.this);

      

selectedValue will return the default if there is no value in it. PS change int to String in the class to get and set the result accordingly.

+1


source







All Articles