【问题标题】:I called an intent to launch activity. But it take more time to launch我调用了启动活动的意图。但启动需要更多时间
【发布时间】:2018-03-29 11:05:29
【问题描述】:

活动需要更多时间才能启动。这是我的代码

 Intent intent =new Intent(getActivity(), McallContactsActivity.class);
 startActivity(intent);

我禁用了即时运行,然后尝试了。但启动活动需要更多时间。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mcall_contacts_activity);
    toolbar = (android.support.v7.widget.Toolbar) 
    findViewById(R.id.toolbar);
    recycleListFriend = (RecyclerView) 
    findViewById(R.id.recycleListFriend);
    swipeRefreshLayout  = (SwipeRefreshLayout) 
    findViewById(R.id.swipeRefreshLayout);
    phoneContact = new PhoneContact();
    if(toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Users");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });

    checkContactPermission();
    setupAdapter();
    phoneContactList = getContacts(McallContactsActivity.this);
    DatabaseReference ref = 
    FirebaseDatabase.getInstance().getReference().child("user");
    ref.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

              dataSnapshot.getValue().keySet().iterator().next().toString();
                    String id = ((HashMap) 
             dataSnapshot.getValue()).keySet().iterator().next().toString();
                    collectPhoneNumbers((Map<String,Object>) 
             dataSnapshot.getValue());
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    //handle databaseError
                }
            });
}

这是 MCallContactsActivity 的 onCreate 方法。此活动没有 onResume() 方法

【问题讨论】:

  • 您的 oncreate() 方法需要更多时间。发布 McallContactsActivity 的 oncreate 方法
  • 发布你的 McallContactActivity.class 的 onCreate 和 onResume 方法
  • 打开一个活动所花费的时间主要是因为 Oncreate 中的代码在新活动上和旧活动的 Onpause 上。
  • 避免在主线程中执行较长的操作。正因为如此,才需要更多时间来启动另一个 Activity。
  • @JyotiJK 我添加了 onCreate 方法。请你检查一下。谢谢

标签: android android-activity launch


【解决方案1】:
class getContects extends AsyncTask<Void,Void,Void>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

        }

        @Override
        protected Void doInBackground(Void voids) {

         // Your Code Here 

            return null;
        }
    }

getContacts 加载太多,这就是活动需要时间加载的原因,因此在创建活动时使用异步任务

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    相关资源
    最近更新 更多