Switching between a tab using the group of operations that it wants to display last - Tab ActivityGroup

I wrote a tab for my Android application.

My question is switching between tabs using the activity group that he wants to display last. I want to show the last opening / visiting screen when we navigate the tab. My first transition to the first screen:

I need to show the last open screen when navigating a tab

Tab 1 โ†’ Sales. This contains 10 screens inside (actiivity) Tab 2 -> Admin. It contains 5 screens inside (actiivity) Tab 3 โ†’ Setting.This contains 8 screens inside. (Actiivity)

I clicked tab 1, it loaded the tab 1 screen, which contains a list of sales route. Then I clicked one sales route, it falls into the list of retailers on the first tab. Then I ended up tab 3 "Settings", complete the work and return to sales. At this time, he should show the last open screen on the "Sales" tab.

When I clicked the tab, it should show the last open activity . How to make?

I liked it. Indicate where I want to change the code for my requirements.

MainActivity.It called after login

public class MainActivity extends TabActivity { int selectedTab; TabHost tabHost ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabview); TabHost t = getTabHost(); tabHost = (TabHost)findViewById(android.R.id.tabhost); TabSpec firstTabSpec = tabHost.newTabSpec("tid1"); TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); TabSpec thirdTabSpec = tabHost.newTabSpec("tid1"); /** TabSpec setIndicator() is used to set name for the tab. */ /** TabSpec setContent() is used to set content for a particular tab. */ firstTabSpec.setIndicator("Sales").setContent(new Intent(this,SalesActivityGroup.class)); secondTabSpec.setIndicator("Admin").setContent(new Intent(this,SettingActivityGroup.class)); thirdTabSpec.setIndicator("Setting").setContent(new Intent(this,SettingActivityGroup.class)); tabHost.addTab(firstTabSpec); tabHost.addTab(secondTabSpec); tabHost.addTab(thirdTabSpec); tabHost.setCurrentTab(0); tabHost.setMinimumHeight(25); } public void onTabChanged(String arg0) { selectedTab = tabHost.getCurrentTab(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { moveTaskToBack(false); return true; } return super.onKeyDown(keyCode, event); } 

}

First Tab1 (Sales) SalesGroupActivity

  public class SalesActivityGroup extends ActivityGroup { public static SalesActivityGroup group; private ArrayList<View> history; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.history = new ArrayList<View>(); group = this; View view = getLocalActivityManager().startActivity("Sales", new Intent(this, SalesRouteActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); replaceView(view); } public void replaceView(View v) { history.add(v); setContentView(v); } public void back() { if (history.size() > 0) { history.remove(history.size() - 1); if (history.size() > 0) { setContentView(history.get(history.size() - 1)); } else { finish(); } } else { finish(); } } @Override public void onBackPressed() { SalesActivityGroup.group.back(); return; } 

Edited This is FirstTab firstActivity - SalesRouteActivity

  public class SalesRouteActivity extends ListActivity{ TableLayout tl; static int positions = 0; static String keyword =""; int uploadSize = 0; private NotificationManager mNotificationManager; private int SIMPLE_NOTFICATION_ID; String strBusinessUnit = ""; String strExecutive = ""; String strTerritoryCode = ""; SimpleAdapter sd; View row = null; View selectRow = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sales_routes); SharedPreferences myPrefs = this.getSharedPreferences("myLogedPrefs",MODE_WORLD_READABLE); strBusinessUnit = myPrefs.getString("BusinessUnit", ""); strExecutive = myPrefs.getString("Executive", ""); strTerritoryCode = myPrefs.getString("TerritoryCode", ""); ArrayList<SalesRoutes> routeList = getSalesRoute(); ArrayList<HashMap<String, String>> routhPath = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < routeList.size(); i++) { if(Integer.parseInt(routeList.get(i).getOutlets()) >0){ HashMap<String, String> map = new HashMap<String, String>(); map.put("routeCode",((SalesRoutes) routeList.get(i)).getRouteCode()); map.put("routeName",((SalesRoutes) routeList.get(i)).getDescription()); map.put("outlets", ((SalesRoutes) routeList.get(i)).getOutlets()); routhPath.add(map); } } ListView list = getListView(); sd = new SimpleAdapter(this, routhPath, R.layout.route_path,new String[] {"routeCode","routeName","outlets" },new int[] { R.id.routeCode,R.id.routeName,R.id.outlets}); row = getLayoutInflater().inflate(R.layout.route_path_row, null, false); getListView().addHeaderView(row); list.setAdapter(sd); list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); list.setSelected(true); list.setTextFilterEnabled(true); list.setItemsCanFocus(true); list.setItemChecked(positions, true); list.setSelectionAfterHeaderView(); if (routeList.size() > 0) { keyword = routeList.get(0).getRouteCode(); } uploadSize = new UploadActivity().getUploadTable(); if (uploadSize > 0) { mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); final Notification notifyDetails = new Notification(R.drawable.icon, "New Alert, Click Me!",System.currentTimeMillis()); Context context = getApplicationContext(); CharSequence contentTitle = "Upload Available..."; CharSequence contentText = "Browse Android Official Site by clicking me"; Intent notifyIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.android.com")); PendingIntent intent = PendingIntent.getActivity(SalesRouteActivity.this, 0, notifyIntent,android.content.Intent.FLAG_ACTIVITY_NEW_TASK); notifyDetails.setLatestEventInfo(context, contentTitle,contentText, intent); mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); } } @SuppressWarnings("unchecked") @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); HashMap<String, String> hashMap = (HashMap<String, String>) l.getItemAtPosition(position); keyword = hashMap.get("routeCode"); positions = position; if(position == 0 ){ }else if(position != 1){ Intent showContent = new Intent(v.getContext(),SalesRouteDevitionActivity.class); Bundle bundle = new Bundle(); bundle.putString("RouteCode", keyword); showContent.putExtras(bundle); getParent().startActivityForResult(showContent, 5); }else{ Intent intent = new Intent(SalesRouteActivity.this, ListRetailerActivity.class); Bundle bundle = new Bundle(); bundle.putString("RouteName", keyword); intent.putExtras(bundle); View view = SalesActivityGroup.group.getLocalActivityManager().startActivity("", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); SalesActivityGroup.group.replaceView(view); } } @Override public void onBackPressed() { SalesActivityGroup.group.back(); } @SuppressWarnings({ "rawtypes", "unchecked" }) public ArrayList<SalesRoutes> getSalesRoute(){ DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(this); try { dbAdapter.createDataBase(); } catch (IOException e) { Log.i("*** select ",e.getMessage()); } dbAdapter.openDataBase(); String sql = "SELECT RouteCode, Description, OutletsAttached " + "FROM WMRoute " + "WHERE ActiveStatus = '1' AND RouteDefaultExecutive = ? AND BusinessUnit = ? AND TerritoryCode = ? " + "ORDER BY RouteCode "; String[]d = new String[]{strExecutive,strBusinessUnit,strTerritoryCode}; ArrayList stringList = dbAdapter.selectRecordsFromDBList(sql, d); dbAdapter.close(); ArrayList<SalesRoutes> salesRoutesList = new ArrayList<SalesRoutes>(); for (int i = 0; i < stringList.size(); i++) { ArrayList<Object> arrayList = (ArrayList<Object>) stringList.get(i); ArrayList<Object> list = arrayList; SalesRoutes salesRoutes = new SalesRoutes(); try { salesRoutes.setRouteCode((String) list.get(0)); salesRoutes.setDescription((String) list.get(1)); salesRoutes.setOutlets((String)list.get(2)); } catch (Exception e) { Log.i("***" + SalesRouteActivity.class.toString(), e.getMessage()); } salesRoutesList.add(salesRoutes); } return salesRoutesList; } } 

Probably my activity groups are created again and again when switching between tabs. Therefore, groups want to create only once and resume when I switch between tabs

Information about each screen / content retrieved from the database.

I encountered this problem for more than 2 days .... Please help me. Please help me with this ....

Thanks in advance.

+3
source share
6 answers

Thanks everyone

There were problems in my MainActivity.

  tabHost = getTabHost(); TabHost.TabSpec spec; Intent intent; intent = new Intent().setClass(this, SalesActivityGroup.class); spec = getTabHost().newTabSpec("Sales").setIndicator("Sales",getResources().getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SettingActivityGroup.class); spec = getTabHost().newTabSpec("Admin").setIndicator("Admin",getResources().getDrawable(R.drawable.admin)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SettingActivityGroup.class); spec = getTabHost().newTabSpec("Setting").setIndicator("Setting",getResources().getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SettingActivityGroup.class); spec = getTabHost().newTabSpec("Inquiry").setIndicator("Inquiry",getResources().getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(0); tabHost.setMinimumHeight(18); tabHost.setFadingEdgeLength(5); tabHost.setFocusable(true); tabHost.requestFocus(); tabHost.setFadingEdgeLength(5); } } 

And I agree @Vaibhav Jani @Dharmendra @Suri, I skipped this onKeyPressed () in all actions.

+1
source

I think you need to override onBackPressed() inside the actions you open in activity-group

Write a code below for each activity you open in an activity group

 @Override public void onBackPressed() { SalesActivityGroup.group.back(); } 

And also replace onBackPressed() with the following code in TABHOST

 @Override public void onBackPressed() { super.onBackPressed(); } 

Good luck

+5
source

The action groups that you created keep your current activity unchanged unless you change it. Therefore, while you are moving between tabs, the activity groups that you assigned to these tabs will not change, their views will remain as they are.

You can try this ....

For each of your activities, you must override onBackPressed() ...

 @Override public void onBackPressed() { ActivityGroupRelatedToThisActivity.group.back(); } 

Also remember that they do not call super.onBackPressed() in any of your activities related to a group of activities

Or

Change private ArrayList<View> history to static private ArrayList<View> history

and

 if(history.size() == 0) replaceView(view); 

in ActivityGroup onCreate() method

+5
source

I think you have a problem in the back () of an ActivityGroup, try this.

 public void back() { if ( history.size() > 1 ) { history.remove(history.size() - 1); View v = arrList.get(history.size() - 1); setContentView(v); } else { this.finish(); } } 
+2
source

This answer will help you. I used group activity for my tabs. Let me know if you still find a problem.

Why is the back button not found in the actions on the muti tab?

+1
source

I saw your update. I do not know how to answer your question. Perhaps you can save information about which view is currently being viewed in onSavedInstanceState , and get the data in onRestoreInstancestate and use it to recreate the view. As a side note, the ActivityGroup deprecated and replaced with the Fragment API.

0
source

All Articles