使用要显示上一个活动的活动组在选项卡之间切换 - Tab ActivityGroup

时间:2011-08-04 04:55:50

标签: android tabs

我为我的Android应用程序编写了标签。

我的问题是在使用要显示上一个活动的活动组的标签之间切换。当我们浏览选项卡时,我想显示上次打开/访问的屏幕。我的第一个屏幕是:

我需要在浏览Tab

时显示上次打开的屏幕

标签1 - >销售。内含10个屏幕(活动) 标签2 - > Admin。这里面包含5个屏幕(活动) 标签3 - > Setting.This里面有8个屏幕。 (actiivity)

我点击了标签1,它加载标签1的屏幕,其中包含销售路线列表。然后我点击了一条销售路线,它进入第一个标签中的零售商列表。然后我将标签3“设置”完成一些工作&回到销售,那时它应该在“销售”标签中显示最后一个打开的屏幕。

当我点击标签时,它应显示上次公开活动怎么办?

我确实喜欢这个。请说明我想要更改我的要求的代码。

MainActivity.It将在登录后调用

  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;
}

被修改 这是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;
    }
}

当您在标签之间切换时,可能会反复创建我的ActivityGroup 因此,当我在标签之间切换

时,群组只想创建一次并恢复

每个屏幕细节/内容都来自数据库..

我面临这个问题超过2天....请帮助我。 请帮助我......

提前致谢.....

6 个答案:

答案 0 :(得分:5)

我认为你必须覆盖onBackPressed()

中正在开展的活动中的activity-group

在活动组

中打开每项活动下面的代码
@Override  
public void onBackPressed() {  
       SalesActivityGroup.group.back();  
} 

并使用 onBackPressed()

中的以下代码替换TABHOST
@Override
public void onBackPressed() {
    super.onBackPressed();
}

祝你好运

答案 1 :(得分:5)

您创建的活动组将保持当前活动视图不会更改,除非您更改它。因此,当您在选项卡之间导航时,您分配给这些选项卡的活动组将不会更改其视图将保持原样。 / p>

你可以试试这个....

对于您的每项活动,您必须覆盖onBackPressed() ...

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

还要记住不要在与活动组相关的任何活动中调用 super.onBackPressed()

private ArrayList<View> history更改为static private ArrayList<View> history

if(history.size() == 0) replaceView(view); 
ActivityGroup的{​​{1}}方法

中的

答案 2 :(得分:2)

我认为你在ActivityGroup的back()中遇到问题,请试试这个。

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

答案 3 :(得分:1)

这个答案对你有所帮助。我已将组活动用于我的标签。如果您仍然发现问题,请告诉我

Why Back button is not detecting in muti tab activities?

答案 4 :(得分:1)

全部感谢;

我的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);
  }
}

我同意@Vaibhav Jani @Dharmendra @Suri,我错过了所有Activity中的onKeyPressed()。

答案 5 :(得分:0)

我看到了你的更新。我不知道如何回答你的问题。也许您可以保存有关onSavedInstanceState中当前显示的视图的信息,并在onRestoreInstancestate中获取数据并使用它来重新创建视图。另请注意,ActivityGroup已弃用,已被Fragment API替换。