0

I have an activity which comprises a custom toolbar, the toolbar contains SearchView. On querying search I am calling following :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Toolbar toolbar = (Toolbar) findViewById(R.id.dashboard_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        drawerListNames = getResources().getStringArray(R.array.drawer_list);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawer = (LinearLayout) findViewById(R.id.left_drawer);
        mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.drawer_list);

        sessionManager = ((RestroOwnerApplication) getApplicationContext()).getSessionManager();

        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Fragment fragment = new SearchFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
//            Toast.makeText(Home.this, query, Toast.LENGTH_SHORT).show();
        }

        addDrawer();
        init();
    }

The fragment appears for a moment. After that activity gets refreshed and fragment disappears. It is reinitializing the activity. I have no idea what am I missing.

Edit: adding methods addDrawer() and init()

    /**
     * Add drawer to dashboard home activity.
     */
    private void addDrawer() {
        mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, drawerListNames));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        final ImageButton navDrawerToggle = (ImageButton) findViewById(R.id.nav_drawer_toggle);
        navDrawerToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final InputMethodManager imm = (InputMethodManager) getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(navDrawerToggle.getWindowToken(), 0);
                mDrawerLayout.openDrawer(GravityCompat.START);
            }
        });
    }

    /**
     * Initialize home screen
     */
    private void init() {
        synchronizeArticles();
    }

    /**
     * Synchronize articles
     */
    private void synchronizeArticles() {
        RestroOwnerApplication restroOwnerApplicationContext =
                (RestroOwnerApplication) this.getApplicationContext();
        this.articleManager = restroOwnerApplicationContext.getArticleManager();
        articleManager.setOnSyncCompletionListener(new ArticleManager.OnSyncCompletionListener() {
            @Override
            public void onSyncCompletion() {
                goTo("browse");
            }
        });
        articleManager.syncArticles();
    }

Edit : demo video https://www.youtube.com/watch?v=Wvhv2jv-RmU

Kuldeep Yadav
  • 1,664
  • 5
  • 23
  • 41
  • what do the 2 methods `addDrawer()` and `init()` do? Perhaps they are modifying the layout? – zec Oct 05 '16 at 13:51
  • adding these two methods – Kuldeep Yadav Oct 05 '16 at 13:52
  • Demo video: https://www.youtube.com/watch?v=Wvhv2jv-RmU – Kuldeep Yadav Oct 05 '16 at 13:57
  • is your search config referencing the same activity that performs the search as the activity that handles the search request too? Android will create a new intent for the search activity and a new instance will be created by default. – zec Oct 05 '16 at 14:08
  • @zec so is there anyway to prevent it? – Kuldeep Yadav Oct 05 '16 at 14:15
  • another stackoverflow [answer](http://stackoverflow.com/questions/7230893/android-search-with-fragments) - someone posted a solution for searching within a fragment. – zec Oct 05 '16 at 14:26

0 Answers0