2017-08-04 7 views
-1

RegistrationActivityの2つのActivitiesがあります。 Intentを使用してボタンをクリックするとがLoginActivityから開始しています。しかし、問題はの遅延が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

アニメーションのために遅延が発生している可能性があります。これを試してみると、遅延が発生するかどうかを確認するためのチェックが行われます。インテントの意図=新しいインテント(LoginActivity.this、RegistrationActivity.class)。 startActivity(インテント); –

+0

私はアニメーションなしで試しました。同じ結果。 – XoXo

+1

getLatLong()を呼び出してください。内部AsyncTaskまたはスレッド。関数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(これは)onCreateを呼び出す必要があります。 –

+0

これは私のために働いていません。アプリケーションが 'NullPointerException'のためにクラッシュする – XoXo

+0

問題を修正しました。しかし、アクティビティの開始にはまだ遅れがあります。 – XoXo

2

実際にはonCreateメソッドで多くのことをロードしようとしています。あなたの活動は創造のために時間がかかります。あなたのケースのフォローactivity life cycle

はすべてsetup()とONSTARTまたは任意の他の適切な方法であなたのasyntaskを置きます。

ありがとうございました。

+0

同じ遅延がありました。 – XoXo

+0

あなたはこの質問にあなたのコードを更新していただけますか? –

+0

更新されたコード – XoXo

関連する問題