0

My Global variables becomes empty after Fragment change. I followed several steps from other's post but still I can't get it. Can anyone please explain and help me to solve this?

GlobalClass.java

public class GlobalClass extends Application{

private static String firstName;
private static String middleName;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getMiddleName() {
    return middleName;
}

public void setMiddleName(String middleName) {
    this.middleName = middleName;
}

}

Activity Java

I store using:

GlobalClass globalVariable = new GlobalClass();
globalVariable.setFirstName(fName.getText().toString());

I also tried putting my GlobalClass name on my Manifest but still nothing happens

<application android:name=".GlobalClass">
Mon Pinoliad
  • 67
  • 1
  • 2
  • 11
  • and how you call them in your other fragment after storing the value of fName ? – Gastón Saillén Feb 14 '18 at 15:33
  • @lDroid I call them using GlobalClass globalVariable= new GlobalClass(); globalVariable.getFirstName(); . But I get "null" – Mon Pinoliad Feb 14 '18 at 15:36
  • If your varaibles are static, then you can have the getter/setters static and access it by name. No need to create a instance of the class everywhere. Also, static vars can be cleared during onPause/onStop of any activity, so it can be a source of your errors, else we need more info on how you are reproducing the error (plus the stacktrace0 – Marcos Vasconcelos Feb 14 '18 at 16:13
  • @MarcosVasconcelos yes maybe it's because I created a Static Var. I tried to Toast it before the first fragment ends, and it successfully echoed on the toast but lost in 2nd fragment. I'm trying to put the name on global var in order to access all stored info after the user finished the form(which is divided by fragments) and put it in the database. – Mon Pinoliad Feb 14 '18 at 16:36

3 Answers3

1

This is wrong:

GlobalClass globalVariable = new GlobalClass();
globalVariable.setFirstName(fName.getText().toString());

Android takes care of creating one instance of the Application you defined in <application android:name=".GlobalClass">. For you, that will be GlobalClass.

Then, you just need to do this to access that one shared Application:

(GlobalClass)context.getApplication

Anyway, you should only extend Application if you need access to the application's lifecycle. In this case, you should not. You are better off creating a simple singleton:

public class GlobalClass{

    private static GlobalClass globalClass;

    private GlobalClass() {
    }
    public static Logger getInstance() {
        if (globalClass == null) {
            globalClass = new GlobalClass();
        }
        return globalClass;
    }
}

And then, use it like this:

GlobalClass.getInstance();
Mikel Pascual
  • 2,202
  • 18
  • 27
0

to send your data with an intent you need to put it in a putExtra, you can do this by fragments doing this

From Activity you can send data to Fragment

Bundle bundle=new Bundle();
bundle.putString("message", "From Activity");
  //set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);

Remember, message is your key, and "From Activity" is the content of message, so you can change "From Activity" with fName.getText().toString() and you will get in your other Fragment the content with this below..

and get it in your Fragment onCreateView

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
          String strtext=getArguments().getString("message");

    return inflater.inflate(R.layout.fragment, container, false);
    }

Normaly from Fragment to Activity you should do this to get your variables content

Intent intent = new Intent(getActivity().getBaseContext(),
                        TargetActivity.class);
                intent.putExtra("message", message);
                getActivity().startActivity(intent);

and get this value in your Activity

Intent intent = getIntent();
String message = intent.getStringExtra("message");

Basically you can do this to put a value and retrieve it

put the value

Bundle bundle = new Bundle();

    bundle.putInt(key, value);
    fragment.setArguments(bundle);

change putInt with putString or whatever you want

retrieve it in your other fragment

Bundle bundle = this.getArguments();
if (bundle != null) {
    int i = bundle.getInt(key, defaulValue);
}

Hope it helps !

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

I faced same problem, I just declare static when declaring the variable and its solved.

public class GlobalVClass extends Application {

    //static added here
    public static String ReturnValue;

    public String getReturnValue() {
        return ReturnValue;
    }

    public void setReturnValue(String returnValue) {
        ReturnValue = returnValue;
    }
}
csalmhof
  • 1,820
  • 2
  • 15
  • 24