-3

For example: I have a String value "A". and I have activities : activity_a, activity_b, activity_C.

Can I use value "A" across all activities? If yes how to achieve this? And not through Intent or send data to another activity.

I am sorry that I am not fluent in English.

I moved a token value in login activity to main activity. I used Intent and move token login activity. this is my login activity code.

StringRequest stringRequest = new StringRequest(Request.Method.POST, serverURL,
                                    new Response.Listener<String>() {
                                        @Override
                                        public void onResponse(String response) {
                                            try{
                                                JSONArray jsonArray = new JSONArray(response);

                                                JSONObject json_code = jsonArray.getJSONObject(0);
                                                JSONObject json_token = jsonArray.getJSONObject(1);

                                                String code = json_code.getString("code");
                                                String token = json_token.getString("token");

                                                Intent myIntent = new Intent(loginActivity.this, mainActivity.class);
                                                myIntent.putExtra("token", token);
                                                startActivity(myIntent);
                                                finish();

                                                overridePendingTransition(R.xml.madefadein, R.xml.splashfadeout);

                                            }catch(JSONException e){
                                                e.printStackTrace();
                                            }
                                        }
                                    }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    switch(error.networkResponse.statusCode)
                                    {
                                        case 409:
                                            Toast.makeText(loginActivity.this, error.networkResponse.toString(), Toast.LENGTH_SHORT).show();
                                            break;
                                    }

                                }

but in main Activity, I tried to declare static like this.

Intent i = new Intent(getIntent());
public static final String token = i.getStringExtra("token");

but it doesn't work.

SunSpike
  • 127
  • 1
  • 1
  • 12

1 Answers1

3

1.just declare your String as public static String strTmp="A"; in your activity than you can use any where in your project

like this

 String strTmp = yourActivity.str; 

2. create a new class like this

public class ServiceClass {  
    public static String strTmp="Data";
}

now you can access this string anywhere in your project like this

String mystr= ServiceClass.strTmp;

3.if you want use hard String than store your string in res/values/string.xml like this

<resources>
  <string name="app_name">PatternView</string>
</resources>

than you can use like this

String str = getResources().getString(R.string.app_name);

4. save it in SharedPreferences like this

code for save data in SharedPreferences like this

SharedPreferences myPref;
SharedPreferences.Editor prefEditor;

myPref = getSharedPreferences("TOKENNAME", MODE_PRIVATE);
prefEditor = myPref.edit();

prefEditor.putString("TOKEN", "your token");
prefEditor.apply();

code for retrieve data from SharedPreferences

SharedPreferences myPref;
myPref = getSharedPreferences("TOKENNAME",
            MODE_PRIVATE);

    String name = myPref.getString("TOKEN", "");
Community
  • 1
  • 1
AskNilesh
  • 67,701
  • 16
  • 123
  • 163