简体   繁体   English

Android App-将对象作为额外的意图传递,而不是使用公共成员变量

[英]Android App- Passing object as intent extra vs using public member variable

I have an android app that has a ListView as its main activity. 我有一个以ListView为主要活动的android应用。 When one of the items is clicked, a new activity is launched, showing a detail view. 单击其中一项时,将启动一个新活动,显示详细视图。 The ListView has an ArrayList of objects of a class that I defined. ListView具有我定义的类的对象的ArrayList。

Currently I pass in these objects to the detail view activity like so: 目前,我将这些对象传递给详细视图活动,如下所示:

Intent intent=new Intent(MainActivity.this,DetailActivity.class);
intent.putExtra(TAG,myList.get(position));
startActivity(intent);

where 'myList' is a private member variable of MainActivity. 其中“ myList”是MainActivity的私有成员变量。 However, the detail view takes some time to load, and I am wondering if that is because the objects in 'myList' contain a Bitmap, which may take some time to read. 但是,详细视图加载需要一些时间,我想知道这是否是因为“ myList”中的对象包含一个位图,这可能需要一些时间才能读取。 The lag might just be due to the emulator running slowly, I am not sure. 我不确定,延迟可能只是由于仿真器运行缓慢所致。

I figured it would be faster to make myList public, and then pass in the position like so: 我认为将myList公开,然后像这样传递位置会更快:

Intent intent=new Intent(MainActivity.this,DetailActivity.class);
intent.putExtra(TAG,position);
startActivity(intent);

and then access the object in DetailActivity like this: 然后像这样在DetailActivity中访问对象:

MyObject object=MainActivity.myList.get(getIntent().getIntExtra(TAG));

However, I believe I have read that there is a chance that my MainActivity may be destroyed when DetailActivity starts, and then wouldn't MainActivity.myList be null? 但是,我相信我已经读到,当DetailActivity启动时,我的MainActivity可能会被破坏,然后MainActivity.myList不会为null吗?

Try to launch your application on a real mobile phone. 尝试在真实的手机上启动您的应用程序。 I had the same problem with the emulator and I think that it was only its fault, because on a real device my code ran really quickly and there was no lag. 我在模拟器上遇到了同样的问题,我认为这只是它的错误,因为在真实设备上,我的代码运行得非常快,并且没有滞后。 The extras on the intent are not such a load that the application gets slow. 意图上的额外负担不会使应用程序变慢。

I figured it would be faster to make myList public 我认为公开myList会更快

it is not just public but also static . 它不仅是public而且是static Don't do that. 不要那样做 It could lead to severe memory leak. 这可能会导致严重的内存泄漏。 Keep providing the object in the Intent , as you are already doing. 像现在一样,继续在Intent提供对象。 If you believe that bitmap could cause some lag, provide just the path to it and have an AsyncTask to load it asynchronously 如果您认为位图可能会导致一些滞后,请仅提供它的路径并让AsyncTask异步加载它

Creating a public fields in Activity is not recomendable. 建议不要在Activity中创建公共字段。 Check activity lifecycle. 检查活动生命周期。

But, I would ask you: 但是,我会问你:

¿Do you need pass the Bitmap in the intent? ¿您是否需要故意传递位图? ¿Can you not obtain it in the second activity, with the position of first? ¿您无法在第二活动中获得第一位置吗?

If you answer is yes. 如果您回答是。 Maybe you can load it in a asynchronous way, with a Handler or a AsyncTask . 也许您可以使用HandlerAsyncTask以异步方式加载它。

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

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