質問

In android TabActivity. I want to reload a tab each time it is visited. for example, if there is a tab that get data from the database, I want to fetch data each time the tab activity is appeared .

Where I need to put the operations that get data from database, on onCreate function, onResume .. ??

役に立ちましたか?

解決

use like this first Tab

public class TabsActivity extends TabActivity {

TabSpec spec1 = tabHost.newTabSpec("tabone");
spec1.setIndicator(createTabView(tabHost.getContext(), "tabone", R.drawable.tab_home));
Intent inte = new Intent(TabsActivity.this, PhotosActivity.class);
inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec1.setContent(inte);


TabSpec spec2 = tabHost.newTabSpec("tabtwo");
spec2.setIndicator(createTabView(tabHost.getContext(), "tabtwo", R.drawable.tab_account));
Intent _int = new Intent(TabsActivity.this, TabTwoActivity.class);
_int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec2.setContent(_int);

tabHost.addTab(spec1);
tabHost.addTab(spec2);

an second Tab

    public class TabTwoActivity extends TabActivity {

   TabSpec spec1 = tabHostacc.newTabSpec("tabone");
spec1.setIndicator(createTabView(tabHostacc.getContext(), "tabone", R.drawable.tab_home));
Intent inte = new Intent(TabTwoActivity .this,  yourfirstActivity.class);
inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec1.setContent(inte);      

TabSpec spec2 = tabHostacc.newTabSpec("tabtwo");
spec2.setIndicator(createTabView(tabHostacc.getContext(), "tabtwo", R.drawable.tab_account));
Intent _int = new Intent(TabTwoActivity .this, yoursecondActivity.class);
_int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec2.setContent(_int);      

tabHostacc.addTab(spec1);
tabHostacc.addTab(spec2);

他のヒント

Put your operations in OnTabChangeListener. Here you can check for the currently selected tab. Do your coding accordingly.

Please check out this

Easy way to achieve this: call finish() in your onPause method of the Activity.

public void onPause() {
    super.onPause();
    finish();
    }

But you shouldnt really do this. Respect the Activity Lifecycle and move the things you need to do whenever the User switches to this Tab from onCreate() to onResume().

To clear things up for you:

-onCreate() gets called the first Time you switch to the Tab - or if it is killed by the system or by calling finish()

-onResume() gets called everytime you switch to the Tab

-onPause() gets called when you switch away from a Tab

Best thing to do:

     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setup your contentview etc
    }
  public void onResume() {
    super.onResume();
    //get your data
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top