Note that there are some explanatory texts on larger screens.

plurals
  1. POShared Preferences in ListFragments
    primarykey
    data
    text
    <p>I wanted to share a string between an main activity and my list fragment. So I thought i can use Shared.Preferences. As i'm new to android and just started using shared preferences, m facing this issue.</p> <p>This is my shared preference class</p> <pre><code>public class AppPrefs { private static final String USER_PREFS = "USER_PREFS"; private SharedPreferences appSharedPrefs; private SharedPreferences.Editor prefsEditor; private String responseXml = "response_xml_prefs"; public AppPrefs(Context context){ this.appSharedPrefs = context.getSharedPreferences(USER_PREFS, Activity.MODE_PRIVATE); this.prefsEditor = appSharedPrefs.edit(); } public String getResponse_xml() { return appSharedPrefs.getString(responseXml, "unknown"); } public void setResponse_xml(String _responser_xml) { prefsEditor.putString(responseXml, _responser_xml).commit(); } } </code></pre> <p>This in my mainActivity</p> <pre><code>Context context = getApplicationContext(); AppPrefs appPrefs = new AppPrefs(context); appPrefs.setResponse_xml(responseXml); </code></pre> <p>and this in my ListFragment</p> <pre><code>Context context = this.getActivity(); AppPrefs appPrefs = new AppPrefs(context); String responseXml = appPrefs.getResponse_xml(); </code></pre> <p>but when i do <code>Log.e("responseXml", responseXml);</code></p> <p>m getting a default string "unknown" which i have soted in the shared preference class. please let me know where have i gone wrong :(</p> <p><strong>Update 1</strong> </p> <pre><code>public class TabActivity extends Activity { MyTask myNewTask; String responseXml; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent in = getIntent(); String requestXml=in.getStringExtra("xml"); myNewTask = new MyTask(requestXml); myNewTask.setOnResultListener(asynResult); myNewTask.execute(); ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayShowTitleEnabled(true); /** Creating All Tab */ Tab tab = actionBar.newTab() .setText("All") .setTabListener(new CustomTabListener&lt;AllMsgFragment&gt;(this, "All", AllMsgFragment.class,responseXml)); //.setIcon(R.drawable.android); actionBar.addTab(tab); /** Creating Success Tab */ tab = actionBar.newTab() .setText("Success") .setTabListener(new CustomTabListener&lt;SuccessMsgFragment&gt;(this, "Success", SuccessMsgFragment.class,responseXml)); //.setIcon(R.drawable.apple); </code></pre> <p>I donno how to pass the bundle</p> <pre><code>} OnAsyncResult asynResult = new OnAsyncResult() { @Override public void onResultSuccess(final int resultCode, final String responseXml) { runOnUiThread(new Runnable() { public void run() { storeData(responseXml); } }); } @Override public void onResultFail(final int resultCode, final String errorMessage) { runOnUiThread(new Runnable() { public void run() { } }); } }; void storeData(String responseXml){ //this.responseXml=responseXml; /*Context context = getApplicationContext(); AppPrefs appPrefs = new AppPrefs(context); appPrefs.setResponse_xml(responseXml);*/ Fragment fragment = new Fragment(); Bundle bundle = new Bundle(); bundle.putString("responseXml", responseXml); fragment.setArguments(bundle); } </code></pre> <p>}</p> <p>its showing null pointer exception</p> <p><strong>Edit 2</strong></p> <p>Using a static variable solved my issue</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload