[英]Best way to store application navigation for Android app?
So my application re-uses quite a few activities. 因此,我的应用程序重用了许多活动。 Originally I was simply adding a "fromClass" line as an extra to Intents as I switched between activities.
最初,我只是在活动之间切换时向Intents添加了一条额外的“ fromClass”行。 The problem now is that I need to know which class I started with several activities ago, in order to display information properly on the current activity.
现在的问题是,为了正确显示有关当前活动的信息,我需要知道我是从几个活动开始的。 I was trying to use an arraylist of strings to store the order and remove them from the list as I went backwards, but I cannot get strings to translate into class names and work correctly.
我试图使用字符串数组列表存储顺序,并在倒退时将其从列表中删除,但是我无法将字符串转换为类名并正常工作。
At the start of the activity I'm adding the following way: 在活动开始时,我将添加以下方式:
Global.appNavigation.add("SecondActivity.class")
When I call onBackPressed() I'm doing this: 当我调用onBackPressed()时,我正在这样做:
/* Remove Last Object (this class) */
Globals.appNavigation
.remove(Globals.appNavigation.size() - 1);
try {
Class<?> c = Class.forName(Globals.applicationNavigation
.get(Globals.appNavigation.size() - 1));
Activity obj = (Activity) c.newInstance();
Intent i = new Intent(mActivity, c);
startActivity(i);
finish();
} catch (ClassNotFoundException e) {
Log.e("Back", "Could not get a class name");
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm constantly getting the ClassNotFoundException. 我一直在收到ClassNotFoundException。 Is there a better way to do this?
有一个更好的方法吗? This application I'm working with is getting quite large, so not calling finish() to an activity may not work in this case.
我正在使用的该应用程序越来越大,因此在这种情况下,不对活动调用finish()可能不起作用。
I ended up calling the "finish()" method, but did a better job keeping track of data that needed to be persistent. 我最终调用了“ finish()”方法,但是在跟踪需要持久化的数据方面做得更好。
Thanks to Ben Von Handorf for the suggestion (see comments 1 and 3 on original question) 感谢Ben Von Handorf的建议(请参阅原始问题的评论1和3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.