Implementing BackStack for Snippets in Android Tabs

My Android application requires an Iphone type design, such as tabs and multiple internal screens. I have finished developing applications and am now watching OutOfMemoryError if I have been using the application for a long time. So I did googling and found that the "Actions inside tabs" setting would consume more memory, and I use replace () in FragmentTransactions to change the fragments. Therefore, when you click on the back key, Fragments images are recreated, which are very expensive in my application, also consume a lot of memory. Therefore, I want to change my application architecture by setting the view directly to tabs and implementing my own back stack to navigate fragments. I implemented the code below and it works fine, but is using show / Hide for the whole application process good or not? Can someone suggest me an example of implementing backstack or fix me in the code below.

package com.fragment; import java.util.ArrayList; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends FragmentActivity implements OnTabChangedListener { ArrayList<Fragment> alTab1 = new ArrayList<Fragment>(); ArrayList<Fragment> alTab2 = new ArrayList<Fragment>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); // Tab for Photos TabSpec photospec = tabHost.newTabSpec("Photos"); // setting Title and Icon for the Tab photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab)); photospec.setContent(R.id.tab1content); // Tab for Videos TabSpec videospec = tabHost.newTabSpec("Videos"); // setting Title and Icon for the Tab videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab)); videospec.setContent(R.id.tab2content); tabHost.addTab(photospec); tabHost.addTab(videospec); tabHost.setOnTabChangedListener(this); FragmentManager fm = getSupportFragmentManager(); if (fm != null) { FragmentTransaction ft = fm.beginTransaction(); Tab1Fragment1 t1f1 = new Tab1Fragment1(); ft.add(R.id.tab1content, t1f1, "t1f1"); addToBackStack(t1f1); ft.commit(); } } public void onTabChanged(String tabId) { if (tabHost.getCurrentTab()==0) { } if (tabHost.getCurrentTab()==1) { if (alTab2.size() == 0) { FragmentManager fm = getSupportFragmentManager(); if (fm != null) { FragmentTransaction ft = fm.beginTransaction(); Tab2Fragment1 t2f1 = new Tab2Fragment1(); ft.add(R.id.tab2content, t2f1, "t2f1"); addToBackStack(t2f1); ft.commit(); } } } } public void addToBackStack(Fragment f) { if (tabHost.getCurrentTab()==0) { if (alTab1.size() > 0) { FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.hide(alTab1.get(alTab1.size() - 1)); ft.commit(); } alTab1.add(f); } if (tabHost.getCurrentTab()==1) { if (alTab2.size() > 0) { FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.hide(alTab2.get(alTab2.size() - 1)); ft.commit(); } alTab2.add(f); } } @Override public void onBackPressed() { if (tabHost.getCurrentTab()==0) { if (alTab1.size() > 1) { FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.remove(alTab1.get(alTab1.size() - 1)); alTab1.remove(alTab1.size() - 1); ft.show(alTab1.get(alTab1.size() - 1)); ft.commit(); return; } } if (tabHost.getCurrentTab()==1) { if (alTab2.size() > 1) { FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.remove(alTab2.get(alTab2.size() - 1)); alTab2.remove(alTab2.size() - 1); ft.show(alTab2.get(alTab2.size() - 1)); ft.commit(); return; } } super.onBackPressed(); } } 
+4
source share

Source: https://habr.com/ru/post/1448027/


All Articles