2017-06-05 4 views
0

Ich habe eine Begrüßungsseite (Launcher) erstellt und einen Fortschrittsbalken eingefügt.Nächste Aktivität wird nach dem Laden dieses Fortschrittsbalkens angezeigt (für einige Sekunden) .Für den ersten Zeit, wenn die App ausgeführt wird, nach ein paar Sekunden wird die nächste Aktivität angezeigt. Jetzt, wenn ich zurück drücken, wird Willkommen Aktivität angezeigt. Aber dieses Mal, nach wenigen Sekunden, ruft es jetzt die nächste Aktivität (Intent) wie beim ersten Mal. Wie löst man das? Der Code ist:Laden der Fortschrittsbalken wird nicht gestoppt, wenn die Aktivität zum zweiten Mal aufgerufen wird

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ProgressBar; 

public class WelcomeActivity extends AppCompatActivity { 
    ProgressBar p; 
    private boolean mbActive; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_welcome); 
     p = (ProgressBar) findViewById(R.id.progressBar); 
     final Thread timerThread = new Thread() { 
      @Override 
      public void run() { 
       mbActive = true; 
       try { 
        int waited = 0; 
        while(mbActive && (waited < 1000)) { 
         sleep(200); 
         if(mbActive) { 
          waited += 200; 
          updateProgress(waited); 
         } 
        } 
       } catch(InterruptedException e) { 
       } finally { 
        onContinue(); 
       } 
      } 
     }; 
     timerThread.start(); 

    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 
    public void updateProgress(final int timePassed) { 
     if(null != p) { 
      final int progress = p.getMax() * timePassed/100; 
      p.setProgress(progress); 
     } 
    } 

    public void onContinue() { 
     Intent intd=new Intent(this,MainActivity.class); 
     startActivity(intd); 
    } 


} 
+0

siehe @Akhil Reddy Sie Call onContinue Methode in OnCreate Methode ..... so, wenn Sie auf die Schaltfläche klicken Aktivität auf Wiederaufnahme heißt ..... Aktivität ruft seine create wieder so, wenn Sie die zweite Aktivität wieder auf Creat setzen möchten e ... Thread-Code in onResume – santoXme

+0

könnte Sie mir auf onResume Beispielcode bereitstellen. Ich bin ein Anfänger. Vielen Dank im Voraus. –

+0

Ich kann Code in Kommentar nicht schreiben, weil Überprüfung der Char-Länge mir nicht erlaubte, also fügte ich eine Antwort dazu hinzu ..... – santoXme

Antwort

1

Try this: --- ich den Code ändern, um den Fortschritt zeigen Code auf halten in auf Lebenslauf

public class WelcomeActivity extends AppCompatActivity { 

    ProgressBar p; 
    private boolean mbActive; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_welcome); 
    } 

    public void updateProgress(final int timePassed) { 
     if (null != p) { 
     final int progress = p.getMax() * timePassed/100; 
     p.setProgress(progress); 
     } 
    } 

    public void onContinue() { 
     Intent intd = new Intent(this, MainActivity.class); 
     startActivity(intd); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     showProgress(); 
    } 

    private void showProgress() { 
     p = (ProgressBar) findViewById(R.id.progressBar); 
     final Thread timerThread = new Thread() { 
     @Override 
     public void run() { 
      mbActive = true; 
      try { 
      int waited = 0; 
      while (mbActive && (waited < 1000)) { 
       sleep(200); 
       if (mbActive) { 
       waited += 200; 
       updateProgress(waited); 
       } 
      } 
      } catch (InterruptedException e) { 
      } finally { 
      onContinue(); 
      } 
     } 
     }; 
     timerThread.start(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 
    } 
+0

Thanq so viel! Es funktioniert perfekt. –

0

sollten Sie verwenden setVisibility(View.GONE);/setVisibility(View.INVISIBLE).

public void onContinue() 
    { 
    p.setVisibility(View.GONE); 
    Intent intd=new Intent(this,MainActivity.class); 
    startActivity(intd); 
    } 

FYI

CalledFromWrongThreadException ist ein häufiger Fehler, wenn Sie versucht haben, von außerhalb des UI-Thread UI Ereignisse an den UI-Thread zu senden.

Für Ihren Crashfall. Lesen runOnUiThread

+0

Wenn ich Addinh diese p.setVisibility (View.GONE), wird die App abgestürzt. –

+0

@AkhilReddy zeigen logcat –

+0

@AkhilReddy hinzufügen 'OnContinue();' Methode in ' –

0

entlassen den Dialog Fortschritt

public void onContinue() 
{ 
p.dismiss(); 
Intent intd=new Intent(this,MainActivity.class); 
startActivity(intd); 
} 
+0

Wo ist 'Progress dialog' ?? –

Verwandte Themen