I ran into a problem in my android app. When I added two tabs, I also had two snippets. But the fact is that the same fragment appears on both tabs. Another fragment is not displayed. Here is how it looks.


Here is my code for my activity:
public class DatabaseFiller extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.rmactivity);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setSubtitle("Created by Rohit Nandakumar");
ActionBar.Tab Frag1 = actionBar.newTab().setText("Search Database");
ActionBar.Tab Frag2 = actionBar.newTab().setText("Show List");
Fragment fragment1 = new DatabaseFillerFragment();
Fragment fragment2 = new DeleteDBItems();
Frag1.setTabListener(new MyTabListener(fragment1));
Frag2.setTabListener(new MyTabListener(fragment2));
actionBar.addTab(Frag1);
actionBar.addTab(Frag2);
}
}
Here is my entire MyTabListener class:
package com.example.foodsaver2;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class MyTabListener implements TabListener {
public Fragment fragment;
public MyTabListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (fragment == null) {
ft.add(android.R.id.content, fragment);
} else {
ft.attach(fragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
ft.detach(fragment);
}
}
}
What am I doing wrong here? Any help regarding this issue would be appreciated.
source
share