简体   繁体   English

Android意向服务onClick,或开始新活动,然后启动Asynctask

[英]Android intent service onClick, or start new activity and then Asynctask

Many times in android apps, users click a button/view and start a new activity, where the new activity pops up in front of what the user was previously looking at, and loads data. 在Android应用程序中,用户多次单击按钮/视图并启动新活动,新活动会在用户之前查看的内容之前弹出并加载数据。

Would there be any difference from a user perspective if the data started loading (from network or disk or both) when the user clicked the button before the next activity started. 从用户的角度来看,如果在下一个活动开始之前用户单击按钮开始加载数据(从网络或磁盘,或从网络和磁盘),是否会有任何区别。 And then that data was returned to the new activity in a broadcast receiver. 然后,该数据在广播接收器中返回到新活动。

This is compared to starting the process in the oncreate of the activity. 这与在活动的oncreate中启动流程进行了比较。 Assuming these network and i/o processes only take milliseconds either way, would it make a difference to the user if the methods were started in onCreate of the new activity, or started in the old activity onClick. 假设这些网络和I / O进程仅以毫秒为单位,那么,如果方法是在新活动的onCreate中启动,还是在旧活动的onClick中启动,则对用户有所不同。

First way, starting I/O and changing views after I/O finishes 第一种方法,启动I / O并在I / O完成后更改视图

//first activity
public void onClick(View v){
     startActivity(new Intent(this, NewActivity.class);
}

//NewActivity.class
onCreate(Bundle mBundle){
   super.onCreate(mBundle);
setContentView(R.layout.mView);
mObject = networkCall();  //after network call, the view objects in this layout will reflect data from the network call

}

second way, starting the I/O in the first activity 第二种方式,在第一个活动中启动I / O

//first activity
public void onClick(View v){
     IntentService networkCall = new IntentService();
     //start network call

     startActivity(new Intent(this, NewActivity.class);
}

//second activity on create just sets the view and also broadcast receiver

My GUESS is that in the split second that it takes for the activity to pop up, the data from the intent service could become available. 我的GUESS是,在弹出活动所需的瞬间,意图服务中的数据将变为可用。 But at the same time, passing data via intent could take just as long making the benefits marginal 但是与此同时,通过意图传递数据可能需要花费同样长的时间才能使收益微不足道

Insight appreciated 洞察力赞赏

In my experience the onCreate() of your new activity is called almost instantly from when you call startActivity(). 以我的经验,从调用startActivity()时起,几乎立即调用了新活动的onCreate()。 The new activity doesn't show up right way because it has to take time to render your layout. 新活动无法正确显示,因为它需要花费一些时间来呈现您的布局。

You could play around with timings yourself by using the Log.d() function. 通过使用Log.d()函数,您可以自己计时。 Something like Log.d(TAG, "This happend at: " + System.currentTimeMillis()); 类似于Log.d(TAG,“这发生在:” + System.currentTimeMillis()); at different points in your code to see when things happen. 在代码的不同位置查看事情何时发生。 Watch the LogCat while your apps runs and you can decide for your self which way is better. 在您的应用程序运行时观看LogCat,您可以自行决定哪种方法更好。

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

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