Skip to content Skip to sidebar Skip to footer

How To Use Variables In Other Classes

Im a little lost on this. Trying to pass variables onto another class for output. This class gets the variable. OneWeekPlan_Start_Btn.java package com.th3ramr0d.prtmanager; import

Solution 1:

You can pass data between two activities using Intent.

Intent intent = newIntent (OneWeekPlan_Start_Btn.this, Save_File.class);
intent.putExtra("wk1day1_inst", wk1day1_ins);
startActivity(intent);

And in your next Activity, use this:

Stringwk1day1_inst= getIntent().getStringExtra("wk1day1_inst");

Hope this helps.

Solution 2:

You can use SharedPreferences to save value of variable. It's advantage over putExtra of Intent is that with SharedPreferences the value of variable can be accessed inside any activity while Intent only allows to access variable in 1 activity.

//Save the value in OneWeekPlan_Start_Btn class
SharedPreferences.Editoreditor= settings.edit();
editor.putString("weekday", wk1day1_inst);
editor.commit();

//Access the value in Save_File classSharedPreferencessettings= PreferenceManager.getDefaultSharedPreferences(this);
Stringwk1day= settings.getString("weekday", "");

Post a Comment for "How To Use Variables In Other Classes"