Search code examples
androidandroid-intentgetstring

how to pass extra intent to two activities


i have an app that on the first activity asks the persons name on the second page it displays the name in a sentence i want to use the name in the third fourth or 9th activity how do i properly declare it (public?) and call it when and where ever i need it? this is my code sending it

Main

public class MainActivity extends Activity {
Button ok;
EditText name;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name=(EditText)findViewById(R.id.editText);
    Typeface font_a = Typeface.createFromAsset(getAssets(),"fonts/MarkerFelt.ttf");
    name.setTypeface(font_a);
    ok=(Button)findViewById(R.id.button);
       Typefacefont_b=Typeface.createFromAsset(getAssets(),
"fonts/SignPaintersGothicShaded.ttf");
    ok.setTypeface(font_b);

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            String nameStr = name.getText().toString();

            Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
            intent.putExtra("NAMEDATA",nameStr);
            startActivity(intent);

        }
    });

}

and this is activity 2 receiving it

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    t = (TextView)findViewById(R.id.textView3);
    Typeface font_b = Typeface.createFromAsset(getAssets(),"fonts/MarkerFelt.ttf");
    t.setTypeface(font_b);
    String n = this.getIntent().getStringExtra("NAMEDATA");
    t.setText(n);

so please how would i reuse this?


Solution

  • Use SharedPreferences to save the name or whatever variable you need, then read whenever you need it. First, create global in MainActivity which will be used as preference file name:

    public static final String PREFS_NAME = "MyPrefsFile";
    

    Then, to save:

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("name", name);
    editor.commit();
    

    to load:

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    String name = settings.getString("name", "John");
    

    Once saved, the prefs are accessible for every activity.

    So in your case, save the name when ok button is pressed:

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            String nameStr = name.getText().toString();
    
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("name", nameStr);
            editor.commit();
    
            Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
            startActivity(intent);
    
        }
    });
    

    Then read it:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    
        t = (TextView)findViewById(R.id.textView3);
        Typeface font_b = Typeface.createFromAsset(getAssets(),"fonts/MarkerFelt.ttf");
        t.setTypeface(font_b);
    
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String n = settings.getString("name", "defaultName");
        t.setText(n);
    

    You can do similarly in every activity you need it. See docs here.