Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:15:00+00:00 2026-06-18T06:15:00+00:00

I have a simple activity which holds a tabbar to switch two fragments. The

  • 0

I have a simple activity which holds a tabbar to switch two fragments. The two fragments are both a listFragment and implement a searchview to make searching possible within the listfragment. The searchview is always shown in the actionbar above the tabbar.

The problem I have is that the input of the searchview isn’t reset once I switch the tab (go to other fragment). Therefore, the second fragment reads the input from the searchview and filters the listfragment accordingly, which actually reads the input I entered when I was still in fragment one.

What I want is the searchview to be a separate searchview for both fragments. Is there a way to achieve this?

Here is my code:

Activity

public class ActivityMainApp extends Activity implements ActionBar.TabListener {

private FragmentManager fragmentManager = getFragmentManager();

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
setContentView(R.layout.mainapp);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Add tabs
    ActionBar.Tab relatieTab = actionBar.newTab().setText("Relaties");
    ActionBar.Tab takenTab = actionBar.newTab().setText("Taken");
    //ActionBar.Tab urenTab = actionBar.newTab().setText("Uren");

    // Listeners 
    relatieTab.setTabListener(this);
    takenTab.setTabListener(this);

    // Tabs toevoegen aan actionbar
actionBar.addTab(relatieTab);
actionBar.addTab(takenTab);

    // Create fragmentmanager to switch fragments
    FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
    Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_content);

    if(fragment == null){
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.fragment_content, new FragRelaties());
    }

}

public void onTabSelected(Tab tab, FragmentTransaction ft) {

    FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();

    if(tab.getText().equals("Taken")){
        FragTaken fragment = new FragTaken();
        fragmentTransaction.replace(R.id.fragment_content, fragment);
        fragmentTransaction.commit();
    }

    if(tab.getText().equals("Relaties")){
        FragRelaties fragment = new FragRelaties();
        fragmentTransaction.replace(R.id.fragment_content, fragment);
        fragmentTransaction.commit();
    }

}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {

}

public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

}

Fragment one

public class FragRelaties extends ListFragment implements SearchView.OnQueryTextListener {

private LayoutInflater inflater;
private ModelRelatie modelRelatie;
private AdapterRelatie relatieAdapter;
public ArrayList<Relatie> relaties;

public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        this.inflater = inflater;

        Activity activity = getActivity();
        final Context context = activity.getApplicationContext();

        setHasOptionsMenu(true);


        new AsyncTask<Void, Void, ArrayList<Relatie>>(){

            protected ArrayList<Relatie> doInBackground(Void... params) {

                // Get all my contacts
                modelRelatie = ModelRelatie.instantiate(context);
                ArrayList<Relatie> relaties = modelRelatie.getRelaties();

                return relaties;

            }
            protected void onPostExecute(ArrayList<Relatie> relaties) {

                // Initial input of objects in the list
                relatieAdapter = new AdapterRelatie(inflater.getContext(), R.layout.relatieslijstitem, relaties);
                setListAdapter(relatieAdapter);

            }

        }.execute();

        View view = inflater.inflate(R.layout.relaties, container, false);

        return view;

}

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {

inflater.inflate(R.menu.relatie_menu, menu);

            // Get the searchview
    MenuItem zoekveld = menu.findItem(R.id.zoekveld_fragrelatie);
    SearchView zoekview = (SearchView) zoekveld.getActionView();

    // Execute this when searching
    zoekview.setOnQueryTextListener(this);

    super.onCreateOptionsMenu(menu, inflater);

}

public void onListItemClick(ListView l, View v, int position, long id) {
   // Things that happen when i click on an item in the list
}

public boolean onQueryTextSubmit(String query) {
    return true;
}

public boolean onQueryTextChange(String zoekterm) {

    Activity activity = getActivity();
    Context context = activity.getApplicationContext();

    // We start searching for the name entered
    ModelRelatie modelRelatie = ModelRelatie.instantiate(context);
    ArrayList<Relatie> relaties = modelRelatie.zoekRelatie(zoekterm);

    // The returned objects are placed in the list
    this.relatieAdapter = new AdapterRelatie(inflater.getContext(), R.layout.relatieslijstitem, relaties);
    setListAdapter(relatieAdapter);

    return true;

}

Fragment Two

public class FragTaken extends ListFragment implements SearchView.OnQueryTextListener {

private LayoutInflater inflater;
private AdapterTaak adapterTaak;
private ModelTaken modelTaken;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        this.inflater = inflater;

        Activity activity = getActivity();
        final Context context = activity.getApplicationContext();

        setHasOptionsMenu(true);

        this.modelTaken = ModelTaken.instantiate(context);
        ArrayList<Taak> taken = modelTaken.getTaken();

        // Initial input for the list
        adapterTaak = new AdapterTaak(inflater.getContext(), R.layout.takenlijstitem, taken);
        setListAdapter(adapterTaak);

        View view = inflater.inflate(R.layout.taken, container, false);
        return view;

}

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {

inflater.inflate(R.menu.taken_menu, menu);

    // get the searview
    MenuItem zoekveld = menu.findItem(R.id.zoekveld_fragtaken);
    SearchView zoekview = (SearchView) zoekveld.getActionView();

    // Execute this when searching
    zoekview.setOnQueryTextListener(this);

    super.onCreateOptionsMenu(menu, inflater);

}

public boolean onQueryTextSubmit(String query) {
    return true;
}

public boolean onQueryTextChange(String zoekterm) {

    Activity activity = getActivity();
    Context context = activity.getApplicationContext();

    // Search the task by the inputed value
    ModelTaken modelTaken = ModelTaken.instantiate(context);
    ArrayList<Taak> taken = modelTaken.zoekTaak(zoekterm);

    // Return the found items to the list
    this.adapterTaak = new AdapterTaak(inflater.getContext(), R.layout.takenlijstitem, taken);
    setListAdapter(adapterTaak);

    return true;

}

}

Both fragments are almost identical except the searching part.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:15:01+00:00Added an answer on June 18, 2026 at 6:15 am

    Its been quite a while but still if anyone is finding the same problem,
    here’s how i managed to do that .

    Just implement onPrepareOptionsMenu() in each of your fragments and
    inside of it, call
    onQueryTextChange(“”); passing “” as query string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity which displays two fragments: When activity is created, it displays
I have two relatively simple codes. One main activity and one intent service. Main
Hi I have an activity which holds a layout, the layout is divided into
I am just started Android application development. Here i have a simple Activity which
I have a simple activity with only one edittext which was set to http://
I have a simple login Activity having two edit texts fileds, for username and
I have an activity which gets called by 2 intents, one after a simple
I have this simple activity which suppose to change the text of TextView once
I have a simple activity in which I start an AsyncTask with a button
simple maybe stupid question. I have a login activity which launches another activity, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.