简体   繁体   English

从另一个活动引用一个活动的属性

[英]Referencing an activity's property from another activity

I have got a class MainActivity (extends AppCompatActivity). 我有一个类MainActivity(扩展了AppCompatActivity)。 In this class a NavDrawerFragment (extends Fragment) mNavigationDrawerFragment is created. 在此类中,创建了NavDrawerFragment(扩展片段)mNavigationDrawerFragment。

Then there is a WpFragment which is started on selection of one of the options of the mNavigationDrawerFragment. 然后有一个WpFragment,它是从选择mNavigationDrawerFragment的选项之一开始的。 This WpFragment starts an AsyncTask WpGetTask. 此WpFragment启动AsyncTask WpGetTask。 On a click event the fragment starts a WpDetailActivity. 在单击事件中,片段将启动WpDetailActivity。 In a special case WpGetTask starts WpDetailActivity. 在特殊情况下,WpGetTask启动WpDetailActivity。

If I now select the home symbol in WpDetailActivity, mNavigationDrawerFragment().openDrawer() of the Main Activity should happen. 如果现在在WpDetailActivity中选择主页符号,则应该发生Main Activity的mNavigationDrawerFragment()。openDrawer()。 I tried it by using a static class and saving mNavigationDrawerFragment there, which worked for me, but another user gets a NullPointerException when mNavigationDrawerFragment of the static class is referenced from WpDetailActivity. 我通过使用静态类并在其中保存mNavigationDrawerFragment进行了尝试,这对我有用,但是当从WpDetailActivity引用静态类的mNavigationDrawerFragment时,另一个用户得到了NullPointerException。

What different ways are there to call mNavigationDrawerFragment of the MainActivity from WpDetailActivity? 从WpDetailActivity调用MainActivity的mNavigationDrawerFragment有什么不同的方法?

You should not assume data in a Fragment or Activity will persist longer than it is visible - they are not designed to persist. 您不应该假设“ Fragment或“ Activity数据的持久时间长于可见数据的时间-它们并非旨在持久化。 A Fragment can access Activity variables that it is attached to, but only during it's lifecycle and only while it is attached to that Activity . Fragment可以访问它附加到的Activity变量,但是只能在其生命周期内并且只能在它附加到Activity

If you want data to be accessed across Activity s then you should either pass the data in an Intent or else use a datastore (like SharedPreferences ). 如果您希望跨Activity访问数据,则应该在Intent传递数据,或者使用数据存储区(如SharedPreferences )。

You also have other options, but the issue here is essentially that once an Activity is not visible to the user, Android may destroy it and re-create it when needed. 您还可以使用其他选项,但实际上,问题是一旦用户看不到Activity ,Android可能会销毁它并在需要时重新创建它。 This is the reason that you will sometimes get NPE's when you try to access a static (or non-static) class / method / variable in an Activity . 这就是为什么当您尝试在Activity访问静态(或非静态)类/方法/变量时有时会获得NPE的原因。

EDIT: 编辑:

Read the "Process Lifecycle" of Activities: 阅读活动的“流程生命周期”:

http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle

"#3" clearly states that Android may "safely kill its process." “#3”明确指出Android可能“安全终止其进程”。 This is not a requirement, it is a possibility. 这不是必须的,这是可能的。 That means, for a stable app, you should assuem that it does happen and plan accordingly. 这意味着,对于稳定的应用程序,您应该确保它确实会发生并相应地进行计划。

I don't know how to re-produce your special nor have your code neither. 我不知道如何重新生成您的special也不知道您的代码。 So it's hard to figure the issue. 因此很难弄清楚这个问题。

About your case, my understand is: you need to open the drawer from WpDetailActivity right? 关于您的情况,我的理解是:您需要从WpDetailActivity打开抽屉,对吗? if so, I would suggest you to use eventbus , send and eventbus notification to MainActivity and call the drawerFragment's openDrawer() method, hope this will help you. 如果是这样,我建议您使用eventbus ,向MainActivity发送和eventbus通知,并调用抽屉片段的openDrawer()方法,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM