I'm working on a project that has tab fragments in android. They are A,B,C and D.So the problem comes when I click tab B there is a button called "like" when this button direct to another activity. There we can like or unlike friends. When the back button clicked on that activity the tab B fragment should be updated automatically.
I referred so much in the internet but couldn't find a suitable answer to me.
I'm very new to android so I'm asking for your help thanks..
I use this code onResume
public void onResume()
{ // After a pause OR at startup
super.onResume();
setbList(bLikedList);
getBLiked();
bListAdapter = new bListAdapter(getActivity(), bLikedList);
listView.setAdapter(bListAdapter);
//Refresh your stuff here
}
Did you try SetCurrentItem(int i) for ViewPager on Call of OnResume()
Related
I have one activity and using fragments, Using same activity all the time and replacing the fragments. I have a customkeyboard in the first fragment.It opens when i click on the edit text.
The problem is when I am in the first fragment and got to settings and changing the language all other views like bunch of textviews and editexts are refreshing to language selected but here custom keyboard is not opening when i click on the edittext.
I tried hiding it on Onresume() if the view instance is already there but nothing is working
Now i want to restart the activity or fragment when i go to setting and change the language. so that the app can start fresh.
#Override
protected void onResume() {
String str = Locale.getDefault().toString();
if(str.equals("de_DE")){
Intent Intent = getIntent ();
finish ();
startActivity ( Intent );
}
super.onResume();
}
Doing the above code in my activity but it is not working
Tried detaching and attaching the fragment also not working.
You can try:
Replacing fragments with FragmentManager (i wouldn't recommend this(it is heavy task resource wise))
Implementing interface for fragments and then you can manually change content for desired language
I am using tab host(tabs) in application, it have four tabs on home screen, I have set new layouts on every tabs, I want to that once user clicking tabs like: tab1>tab2>tab3>tab4 and press back on tab4 he/she should go back to tab3 and on tab3 pressing back go to tab2 and so on. I didn't get any idea, how to implement? Could any one help me on this, any help will be appreciated. Thanking you all..!!
Create the below method in your class which extends TabActivity,
public void switchTabBar(int tab) {
tabHost.setCurrentTab(tab);
}
And now in your activity's back press method, do this,
public void onBackPressed() {
TabBar parentTab = (TabBar) this.getParent();
parentTab.switchTabBar(1);//Instead of 1 provide the tab position which you want to navigate to.
}
you can override onBackPressed() and inside it do what you want.
I dont know why you want to add back button.In tabs there's no need to add back button.See the below link
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
You need to create some sort of a activities-backstack (if you use nested ActivityGroup), override onBackPressed() method in your TabActivity and manage this backstack in onBackPressed() method.
You have 2 options:- with Tab or without Tab.
If it is not necessary that you have to use tab in your code then instead of tab you can use buttons and you can easily get your goal. But if you want to use tab then you have to Override onBackPressed() in every 4 Activity.
I hope you got any idea according to my answer. I hope it helps you.
I have an application first showing a login screen which on a succesful login changes to a tab view with 3 tabs. Each tab is containing a different activity. When I switch between the tabs I would like the back button to return to the previous tab instead of the login screen.
How should the be done? I'm considering implementing onBackPressed on the tab view and remembering the tab-stack manually but I don't feel that is the right approach.
Let say you have tabs created in class called Main.java. Create there method like this:
public void switchTab(int tab) {
tabHost.setCurrentTab(tab);
}
which will change current tab. Next in activity where you want to change navigation insert the following code:
#Override
public void onBackPressed() {
Main m = (Main)getParent();
m.switchTab(0);
}
This should work.
I stuck with one problem. actually my screen consists of two tabs. under each tab i have 4-4 activity. i m displaying each activity with the help of activity group in single tab.
Suppose i m in 1st tab which is active. Under this tab i m on 2nd activity(e.g first activity is list activity and second activity gives the result from the first activity)
I want when i click on 1st tab again it should show me the first activity again without using back button.?
I had that problem sometime ago... and that happens because people like to emulate the bottom bar of the iPhone. Android apps don't work that way and using Activity Group is always a signal of a poor UI design.
Anyway, this is what I did:
tabHost.setCurrentTabByTag(TAB_ID_MORE);
tabHost.getCurrentTabView().setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if( MoreGroupActivity.self != null ) {
MoreGroupActivity.self.reset();
}
tabHost.setCurrentTabByTag(TAB_ID_MORE);
}
});
tabHost.setCurrentTabByTag(TAB_ID_HOME);
The above code is not generic, but will give you an idea of the workaround I found. Let me explain:
tabHost.setCurrentTabByTag(TAB_ID_MORE); I use this to select a current tab (in my case, the main tab was another tab, so I had to to this and then change back with tabHost.setCurrentTabByTag(TAB_ID_HOME);). I mean, in my case, the only tab with that behavior was the "More" tab.
tabHost.getCurrentTabView().setOnClickListener this allows you to put a listener to the tab. As you may have already noticed, using OnChangeTabListener is not an option in this kind of situation.
MoreGroupActivity.self Inside my group activity, I had a static field referencing the group activity it self. This kind of hacks are common while using this crappy approach.
tabHost.setCurrentTabByTag(TAB_ID_MORE); this reset the tab so that it can change back to your first activity.
When you are adding a new TabHost.TabSpec to the TabHost
use
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
to the respective Intent
I've put a little app together that has three tabs to show three different web pages. It does work however I am bit worried I haven't got enough control over how this whole thing works. When I click a tab, I get a web page loaded (see code sample below), now when I click another tab another page loads in another view. When I go back to the first tab, the whole thing get initilized again and the page loads. Is there a way how I can control this and keep the underneeth tab's activity in its current state as long as I want (and say only "refresh" the page when it changes).
do I need to handle onPause()/onResume() methods for that or instead implement my tabs as views of a single activity (is this possible at all?)? How do I store the state of my activity to avoid re-initializing it every time?
this how activities are hooked to tabs:
intent = new Intent().setClass(this, tab_schedule.class);
spec = tabHost.newTabSpec("Schedule").setIndicator("Schedule",
res.getDrawable(R.drawable.tab_icon_schedule)).setContent(
intent);
tabHost.addTab(spec);
the tab_schedule.class does a simple web page load:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_people);
try {
WebView currentView = (WebView) findViewById(R.id.tab_people_WebView);
currentView.getSettings().setJavaScriptEnabled(true);
currentView.loadUrl("http://pda.lenta.ru");
} catch (Exception e) {
Log.v("webClientInit", e.getMessage());
}
}
If you don't want to create a new activity for each tab, you can use a TabWidget with a FrameLayout to toggle between views.
As to switching activities, see this question for a way to not recreate the activity each time.
Regardless, you should always implement onPause and onResume to restore the state of your app. You might want to read up on the Activity Lifecycle, but basically you cannot prevent your activity from being killed if goes into the background. Thus, you should store your state in onPause. The link above has some info on how to do so as well.
To bring the previous activity to the top of the stack use intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);