简体   繁体   中英

Getting blank screen when switching between Activities ( Activity with services)?

In my app i am using Services for some requirements, When i am switching from Activity1 to Activity2 i am getting blank screen for few seconds. In Activity2 having some services, This is the code i am using

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(isOnline()){

    }else{ 
        alert();
    }
    dialog = new ProgressDialog(this);   
    dialog.setCancelable(false);
    app_preferences = new AppPreferences(this);
    new LongOperation().execute();

    setContentView(R.layout.activity_accounts); 
}

Using asynctask to start services from onCreate(). new LongOperation().execute();

private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {          
        ProgressBar_show();
    }
    @Override
    protected String doInBackground(String... params) { 
        LoggingAccounts();
        return null;
    }
    @Override
    protected void onPostExecute(String result) {        
        ProgressBar_hide();
    } 
}

private void LoggingAccounts() {

    Account_Added_Log_States();

    ArrayList<AccountInfo> _Accounts_list = new ArrayList<AccountInfo>();
    accounts_data = new AccountDataSource(this);
    accounts_data.open();       
    _Accounts_list = accounts_data.getAllAccounts();
    accounts_data.close();       
    Log.i(TAG, "No of acc's-" + _Accounts_list.size()); 

    for(AccountInfo acc : _Accounts_list) {
        Log.i(TAG, "Acc type " +acc.getAcc_Type());

        if(acc.getAcc_Type().equals(UsedStrings.GoogleAccount) && Gtalk_Log_State ) {
            /////// service start
            final Intent gtalk_intent = new Intent(AccountsActivity.this, GtalkService.class);
            gtalk_intent.putExtra("Key", "gtalk service*****");
            gtalk_intent.putExtra("user_name", acc.getAcc_Name());
            gtalk_intent.putExtra("user_pass", acc.getAcc_Pass());
            Thread t = new Thread(){
                public void run(){
                    startService(gtalk_intent);
                }
            };
            t.start(); 
        }
    }
}

Like this i am using 3 different services in this activity. when i am switching from activity getting blank screen.

The only problem which I am seeing here is you called the AsyncTask first and Set the content View which in will call the UI thred then Background and then again UI thread. and after that you care setting the ContentView so what will happen is the Progress Dialog is waiting for UI thread to get render which will be after setConterView.

Swap the below lines

new LongOperation().execute();

 setContentView(R.layout.activity_accounts); 

to

setContentView(R.layout.activity_accounts); 
 new LongOperation().execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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