-1

I have a form in which I'm filling the details & to get the location I'm using Google Maps. But when I get back to the activity after getting the location from maps all fields are null.

How do I store the state before moving to maps and get that exact state after coming back from maps activity?

EDIT: Fragment's onCreate

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    // Here I used recreate but it didn't work
    // getActivity.recreate();

    View view = inflater.inflate(R.layout.fragment_login, container, false);
    ButterKnife.bind(this, view);
    return view;
}

From here i'm going to map Activity

@OnClick(R.id.frag_expense_lllocation)
    public void getLocation(){
        UiActivity.startMapActivity(context);
    }

and to get back to current activity with selected location

 double lat = marker.getPosition().latitude;                                                            
 double lng =     marker.getPosition().longitude;                                                            
 String position = lat + "," + lng;        
 UiActivity.startExpenseActivity(getContext(), position); 
 getActivity().finish();
batman
  • 1,937
  • 2
  • 22
  • 41
rookieDeveloper
  • 2,459
  • 1
  • 22
  • 44

2 Answers2

0

@gaurav you can use onSaveInstanceState() and onRestoreInstanceState() callback to accomplish this task very easily.

In first callback you need to save the state which you want to save and from second you can restore the state. for further information you can check answer of How to use onSaveInstanceState() and onRestoreInstanceState()

Community
  • 1
  • 1
Alok
  • 881
  • 1
  • 11
  • 18
0

As your activity begins to stop, the system calls onSaveInstanceState() so your activity can save state information with a collection of key-value pairs. The default implementation of this method saves information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView.

To save additional state information for your activity, you must implement onSaveInstanceState() and add key-value pairs to the Bundle object. For example:

You need to override

 onSaveInstanceState(Bundle savedInstanceState)

write the application state values you want to change to the Bundle parameter like this:

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

Restore Your Activity State

When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

For example, here's how you can restore some state data in onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}
shekhar pande
  • 1,172
  • 1
  • 11
  • 21