Search code examples
androideclipseandroid-tabhost

How to Implement shared preference concept in tabhost on android eclipse


I am self- learner to android,

Assume an android program is going to display some result on a textview.can any one tell me how to show that answer on first tab of the tab host's on the next screen.How to achieve this?

As per my knowledge i googled and found "Shared preference" concept will be helpful to this problem. Was i right?

And i found some samples but they are not making me clear,can any one give me some examples with screen images.

Thanks for your precious time!.


Solution

  • here is a small sample in this whatever you enter in first tab it is shown on second tab:

    Main class

    public class CheckkActivity extends TabActivity {
     /** Called when the activity is first created. */
      @Override      
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources();                 
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab
    
    
        intent = new Intent().setClass(this, NewActivity.class);
    
    
        spec = tabHost.newTabSpec("first").setIndicator("First")
                      .setContent(intent);
        tabHost.addTab(spec);
    
    
        intent = new Intent().setClass(this, SecondActivity.class);
        spec = tabHost.newTabSpec("second").setIndicator("Second")
                      .setContent(intent);
        tabHost.addTab(spec);
    
    
    
        tabHost.setCurrentTab(0);
    
    }
    }
    

    NewActivity

         public class NewActivity extends Activity{
        EditText get;
        Button save;
         SharedPreferences sharedPreferences;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.next);
            get=(EditText)findViewById(R.id.next);
            save=(Button)findViewById(R.id.button1);
    
    
              save.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(get.getText().toString().equalsIgnoreCase("")){
                        Toast.makeText(getApplicationContext(), "enter something", Toast.LENGTH_SHORT).show();
                    }else{
                    sharedPreferences=PreferenceManager.getDefaultSharedPreferences(NewActivity.this);
                       Editor editor1 = sharedPreferences.edit();
                       editor1.remove("answer");
                       editor1.commit();
                     sharedPreferences=PreferenceManager.getDefaultSharedPreferences(NewActivity.this);
                      Editor editor = sharedPreferences.edit();
                      Log.i("set value",""+get.getText().toString());
                      editor.putString("answer",get.getText().toString());
                      editor.commit();}
                }
            });
    
        }
    
       }
    

    SecondActivity

          public class SecondActivity extends Activity{
    TextView set;
    SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        set=(TextView)findViewById(R.id.second);
    
    }
    
         @Override
         protected void onResume() {
    // TODO Auto-generated method stub
    sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(SecondActivity.this);
    String answer= sharedPreferences.getString("answer", "");
    Log.v("get value",""+answer);
    if(answer.equalsIgnoreCase(null)){
        set.setText("nothing to display");
    }else{
    set.setText(answer);
    }
    super.onResume();
         }
       }