I am creating a tab application, using fragments.
I have successfully created tabs, but now I need to create an object when a specific fragment is selected. e.g. When I select SettingsTab
I need to call a function that is defined in Test class
. and similarly destroy
// SettingsTab
package com.example.sensorspositioningn;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SettingsTab extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.settingstab, container, false);
}
}
Here is the code in mainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appContext = getApplicationContext();
//ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab Tab1= actionbar.newTab().setText("Tab1");
ActionBar.Tab Tab2= actionbar.newTab().setText("Tab2");
ActionBar.Tab Tab3= actionbar.newTab().setText("Tab3");
ActionBar.Tab SettingsTab = actionbar.newTab().setText("Settings");
Fragment Tab1F= new Tab1();
Fragment Tab2F= new Tab2();
Fragment Tab3F= new Tab3();
Fragment mSettingsFragment = new SettingsTab();
Tab1.setTabListener(new MyTabsListener(Tab1F));
Tab1.setTabListener(new MyTabsListener(Tab2F));
Tab1.setTabListener(new MyTabsListener(Tab3F));
SettingsTab.setTabListener(new MyTabsListener(mSettingsFragment));
actionbar.addTab(Tab1);
actionbar.addTab(Tab2);
actionbar.addTab(Tab3);
actionbar.addTab(SettingsTab);
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.
fragment:fragment:1.1. 0 you can just use onPause() and onResume() to determine which fragment is currently visible for the user. onResume() is called when the fragment became visible and onPause when it stops to be visible.
In your ActionBar.TabListener
you can do this - (I'm assuming that you need the object to be in the Activity
)
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(fragment instanceof SettingsTab) {
// Create your object, call your function
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
if(fragment instanceof SettingsTab) {
// Destroy your object
}
}
}
Try this
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(fragment instanceof SettingsTab){
doSettingsMethod();
}
if(fragment instanceof Tab1){
doOtherMethod1();
}
if(fragment instanceof Tab2){
doOtherMethod2();
}
if(fragment instanceof Tab3){
doOtherMethod3();
}
}
Also you may change TabListener class in this way. As for me it's a better design.
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
private final Runnable onSelect;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
this.onSelect = null;
}
public MyTabsListener(Fragment fragment, Runnable onSelect) {
this.fragment = fragment;
this.onSelect = onSelect;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(onSelect != null){
fragment.getActivity().runOnUiThread(onSelect);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
The only official source I found to this was on the developer site: http://developer.android.com/training/implementing-navigation/lateral.html
They simply put arguments for each created fragment, and read the arguments when the fragments get selected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With