简体   繁体   English

为Android应用程序存储应用程序导航的最佳方法?

[英]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.

相关问题 在Android应用程序中存储文本数据的最佳方法? - Best way to store text data in an android app? 这是在Android应用中存储“引号”的最佳方法 - Which is the best way to store “quotes” in an android app 在Android应用中存储单个用户的最佳方法? - Best way to store a single user in an Android app? Android:在Android中为应用程序离线存储JSON数据的最佳方法是什么? - Android: what is the best way to store JSON data offline for the app in android? 在移动Web应用程序中存储“应用程序机密”的最佳方法? - The best way to store “app secret” in a mobile web application? 在Android应用程序中存储多个用户数据的最佳方法 - Best way to store multiple users's data in android application 在 Android 应用程序中存储静态数据的最佳方式是什么? - What is the best way to store static data in Android application? 在 android 应用程序中存储数据(大量带有图像的字符串)的最佳方法 - Best Possible way to Store Data (Lot of Strings with Images) in android application 哪种方式最适合在Android中存储我们的应用程序设置 - which way is best to store our application settings in Android 为 android 应用程序存储 static 数据的最佳方式是什么? - What is the best way to store static data for an android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM