Samer Alabi Samer Alabi - 27 days ago 12
Android Question

Android - String value from AlertDialog keeps resetting on access

I'm having an issue where I enter data into where my string keeps resetting to null even after I have entered data for it in my AlertDialog Fragment. What's supposed is that I enter data into a EditText object, store it in a string variable, set it as the string value in my Getter/Setter class then retrieve from that class in my fragment.

Image of AlertDialog

null



AlertDialog Fragment

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import org.ramferno.scoutapplication.ramfernoscout.R;
import org.ramferno.scoutapplication.ramfernoscout.providers.AddressProvider;

public class AddressDialogFragment extends DialogFragment {
AddressProvider addressProvider = new AddressProvider();
EditText enterIP;
String urlAddress;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//Declare and initialize objects
LayoutInflater i = getActivity().getLayoutInflater();
View v = i.inflate(R.layout.fragment_dialog, null);
enterIP = (EditText) v.findViewById(R.id.enterIP);

//Create AlertDialog
AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
.setTitle("Enter IP Address")
.setPositiveButton("ADD",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
urlAddress = enterIP.getText().toString();
addressProvider.setAddress(urlAddress);
} //End of onClick
}) //End of DialogInterface
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
} //End of onClick
} //End of DialogInterface
); //End of AlertDialog
b.setView(v);
return b.create();
} //End of onCreateDialog
} //End of class


Getter/Setter class

public class AddressProvider {
private String urlAddress;

public String getAddress() {
return urlAddress;
} //End of getAddress

public void setAddress(String urlAddress) {
this.urlAddress = urlAddress;
} //End of setAddress
} //End of class


ScoutFragment (Fragment that receives string from Getter/Setter)

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import org.ramferno.scoutapplication.ramfernoscout.downloaders.Downloader;
import org.ramferno.scoutapplication.ramfernoscout.R;
import org.ramferno.scoutapplication.ramfernoscout.providers.AddressProvider;

//Start of ScoutFragment
public class ScoutFragment extends Fragment {
//Declares Android UI objects
AddressProvider addressProvider = new AddressProvider();
FloatingActionButton addDataScout;
ListView eListScoutInfo;
String IP = addressProvider.getAddress();

//Declare and initialize variable
String urlAddress = "http://" + IP + "/ramfernoscout/matchdb/matchretrieve.php";

public ScoutFragment() {
// Required empty public constructor
} //End of ScoutFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflates layout for this fragment
View view = inflater.inflate(R.layout.fragment_scout, null, false);

//Instantiate ListView object with the xml ListView object
eListScoutInfo = (ListView) view.findViewById(R.id.listScoutInfo);

//Add instructions to the Refresh FAB that will download the data from the database server
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab2);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Downloader d = new Downloader(getActivity(),urlAddress,eListScoutInfo);
d.execute();
} //End of onClick
}); //End of setOnClickListener

//Change fragment to AddScoutDataFragment with animations
addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
addDataScout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddScoutDataFragment fragment = new AddScoutDataFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right,
R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} //End of onClick
}); //End of setOnClickListener

//Returns view
return view;
} //End of onCreateView
} //End of class

Answer

You are using AddressProvider addressProvider = new AddressProvider(); in both ScoutFragment and AddressDialogFragment.

The new operator will create a new instance of AddressProvider class. If you want to persist the data, you should create only a single instance of AddressProvider. So you should make AddressProvider a SingleTon class.

public class AddressProvider {
    private static AddressProvider ourInstance = new AddressProvider();
    private String urlAddress;

    private AddressProvider() {
    }

    public static AddressProvider getInstance() {
        return ourInstance;
    }

    public String getAddress() {
        return urlAddress;
    }

    public void setAddress(String urlAddress) {
        this.urlAddress = urlAddress;
    }
}

Usage,

To store the IP,

AddressProvider.getInstance().setAddress("xxx.xxx.xx.xx");

to retrieve,

AddressProvider.getInstance().getAddress()
Comments