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. 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:
/* 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. 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.
I ended up calling the “finish()” method, but did a better job keeping track of data that needed to be persistent.
Thanks to Ben Von Handorf for the suggestion (see comments 1 and 3 on original question)