2017-08-04 52 views
-1

一個活動,我有兩個Activities名爲LoginActivityRegistrationActivity。我從LoginActivity開始RegistrationActivity使用Intent單擊按鈕。但問題是有一個延遲2到3秒加載RegistrationActivity。可能是什麼問題?延遲,同時開始從另一個

LoginActivity

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_login); 
     setup(); 
    } 

    private void setup() { 
     layout = (RelativeLayout) findViewById(R.id.loginLayout); 
     register = (Button) findViewById(R.id.registerText); 
     email = (AppCompatEditText) findViewById(R.id.loginEmail); 
     password = (AppCompatEditText) findViewById(R.id.loginPassword); 
     submit = (Button) findViewById(R.id.loginSubmit); 
     submit.setOnClickListener(listener); 

     register.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(LoginActivity.this, RegistrationActivity.class); 
       ActivityOptionsCompat options = ActivityOptionsCompat.makeScaleUpAnimation(view, 0, 0, view.getWidth(), view.getHeight()); 
       ActivityCompat.startActivity(LoginActivity.this, 
         intent, options.toBundle()); 
      } 
     }); 
    } 

RegistrationActivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_registration); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    setup(); 
    new getLatLongAsync().execute(); 
} 

private class getLatLongAsync extends AsyncTask<String, Void, String> { 

    @Override 
    protected void onPreExecute() { 

    } 

    @Override 
    protected String doInBackground(String... args) { 


     mGoogleApiClient = new GoogleApiClient.Builder(RegistrationActivity.this) 
       // The next two lines tell the new client that 「this」 current class will handle connection stuff 
       .addConnectionCallbacks(RegistrationActivity.this) 
       .addOnConnectionFailedListener(RegistrationActivity.this) 
       //fourth line adds the LocationServices API endpoint from GooglePlayServices 
       .addApi(LocationServices.API) 
       .build(); 

     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1000); // 1 second, in milliseconds 

     return null; 
    } 

    protected void onPostExecute(String args) { 
     mGoogleApiClient.connect(); 
    } 
} 

getLatLong()獲得緯度和經度:

private void getLatLong() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       // The next two lines tell the new client that 「this」 current class will handle connection stuff 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       //fourth line adds the LocationServices API endpoint from GooglePlayServices 
       .addApi(LocationServices.API) 
       .build(); 

     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1000); // 1 second, in milliseconds 
    } 
+0

可能是由於製作動畫是越來越delay.check嘗試這種只看到如果獲取延遲與否,意向意圖=新意圖(LoginActivity.this,RegistrationActivity.class); startActivity(intent); –

+0

我試過沒有動畫。相同的結果。 – XoXo

+1

請致電getLatLong();在AsyncTask或Thread中。調用函數getLatLong();可能會延遲負載。 – Sanil

回答

1

RegistrationActivity:更新的代碼

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      setContentView(R.layout.activity_registration); 
      setup(); 
      //calling asynctask.. 
      new getLatLongAsync().execute(); 
     } 

     private void setup() { 
      layout = (RelativeLayout) findViewById(R.id.registerLayout); 
      language = (Spinner) findViewById(R.id.lang); 
      email = (EditText) findViewById(R.id.regEmail); 
      password = (EditText) findViewById(R.id.regPassword); 
      nickname = (EditText) findViewById(R.id.regNickname); 
      username = (EditText) findViewById(R.id.regUsername); 
      dob = (EditText) findViewById(R.id.regDOB); 
      submit = (Button) findViewById(R.id.regSubmit); 
      prefs = new SecurePreferences(this); 
      progressDialog = new ProgressDialog(this); 
      progressDialog.setMessage("Loading...."); 
      language.setOnItemSelectedListener(listener); 
      submit.setOnClickListener(onClickListener); 

      languages = new ArrayList<>(); 
      languages.add("Select Language"); 
      languages.add("English"); 
      languages.add("Tamil"); 
      languages.add("Malayalam"); 

      adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, languages); 
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      language.setAdapter(adapter); 
     } 


    class getLatLongAsync extends AsyncTask<String, Void, String> { 

      @Override 
      protected void onPreExecute() { 

      } 

      @Override 
      protected String doInBackground(String... args) { 


       getLatLong(); 


       return null; 
      } 

      protected void onPostExecute(String args) { 
      } 
     } 

private void getLatLong() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       // The next two lines tell the new client that 「this」 current class will handle connection stuff 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       //fourth line adds the LocationServices API endpoint from GooglePlayServices 
       .addApi(LocationServices.API) 
       .build(); 

     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1000); // 1 second, in milliseconds 
    } 
+0

mGoogleApiClient = new GoogleApiClient.Builder(this)should call onCreate。 –

+0

這不適合我。由於'NullPointerException'應用程序崩潰, – XoXo

+0

我糾正了這個問題。但是仍然存在啓動活動的延遲。 – XoXo

2

其實你要加載很多事情上onCreate方法。由於您的活動需要時間進行創作。

關注activity life cycle

對於您的情況下把所有setup()和您在onStart或其他任何適當的方法asyntask。

非常感謝。

+0

我試過了,但有相同的延遲。 – XoXo

+0

你可以請更新你的代碼到這個問題? –

+0

更新代碼請檢查 – XoXo