This Navigator class helps to easily navigate from one activity to another including passing parameters. It also dose the navigate back to a previous activity as well. With the use of the HashMap object you can pass
serializable objects to other activities.
public class Navigator {
//navigate form one activity to another with passing parameters
public static void Navigate(Activity activityFrom, Class classTo,
HashMap<String, Serializable> params) {
Controller.instance().PrevActStack().push(
activityFrom.getClass().getName());
Intent intentNavigateTo = new Intent(activityFrom, classTo);
intentNavigateTo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Set set = params.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry me = (Map.Entry) iterator.next();
intentNavigateTo.putExtra((String) me.getKey(), (Serializable) me
.getValue());
}
activityFrom.startActivityForResult(intentNavigateTo, 0);
activityFrom.finish();
params.clear();
}
//navigate form one activity to another without passing parameters
public static void Navigate(Activity activityFrom, Class classTo) {
Controller.instance().PrevActStack().push(
activityFrom.getClass().getName());
Intent intentNavigateTo = new Intent(activityFrom, classTo);
intentNavigateTo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityFrom.startActivity(intentNavigateTo);
activityFrom.finish();
}
//navigate back to previous activity with passing parameters
public static void NavigateBack(Activity activityFrom,
HashMap<String, Serializable> params) throws Exception {
if (Controller.instance().PrevActStack().size() == 0) {
throw new Exception("No previous activity found !!");
}
Intent intentNavigateBackTo = new Intent(activityFrom, Class
.forName(Controller.instance().PrevActStack().pop()));
intentNavigateBackTo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Set set = params.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry me = (Map.Entry) iterator.next();
intentNavigateBackTo.putExtra((String) me.getKey(),
(Serializable) me.getValue());
}
activityFrom.startActivity(intentNavigateBackTo);
activityFrom.finish();
params.clear();
}
//navigate back to previous activity without passing parameters
public static void NavigateBack(Activity activityFrom) throws Exception {
if (Controller.instance().PrevActStack().size() == 0) {
throw new Exception("No previous activity found !!");
}
Intent intentNavigateBackTo = new Intent(activityFrom, Class
.forName(Controller.instance().PrevActStack().pop()));
intentNavigateBackTo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityFrom.startActivity(intentNavigateBackTo);
activityFrom.finish();
}
}
No comments:
Post a Comment