2017-07-29 6 views
0

Ich habe eine app, in dem die Navigation zu den Aktivitäten im Grunde ist wie folgt gegliedert:Android Aktivität zeigt noch alte Inhalte

MainActivity -> Activity1 -> Activity2.

In MainActivity öffnet der Benutzer eine Datei, die in Activity1 angezeigt/erkundet wird. In Activity2 werden weitere Informationen über die Datei auf der Basis der Aktion des Benutzers in Activity1 angezeigt.

Aktivität1 hat android:launchMode="singleTop". Wenn Sie also von Activity2 zu Activity1 zurückkehren, bleibt der Status erhalten.

Jetzt habe ich in beiden Activity1 und Activity2 eine "Exit" -Schaltfläche eingefügt, um zu MainActivity zurückzukehren und eine neue Datei zu öffnen.

Leider, wenn ich die neue Datei öffne, zeigt Activity1 überlappende Informationen über die neue Datei und die vorherige Datei an. Wie konnte ich Activity1 vermeiden, um die vorherige Instanz zu verfolgen, wenn ich sie von MainActivity starte? Vielen Dank im Voraus.

+0

Wenn Sie von Activity1 zu Activity2 wechseln, beenden Sie die Aktivität, damit wird Ihr Problem gelöst. Für z.B. startActivity (Absicht); Fertig(); –

Antwort

1

, warum Sie nicht nur die Aktivität starten und anruf() auf Hörer

// MainActivity 
Intent intent = new Intent(this, YourActivity.class); 
this.startActivity(intent); 

//Activity 1 
finish(); 

oder wenn Sie beide in MainActivity behandeln möchten können Sie die folgende tun, können Sie es auch anpassen Parameter für die Bereitstellung von Karten mit putExtra() -Methode.

// MainActivity 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // Check which request we're responding to 
     if (requestCode == 1) { 
      // Make sure the request was successful 
      if (resultCode == RESULT_OK) { 
       // Do your stuff here 
      } 
     } 
     if (requestCode == 2) { 
      if (resultCode == RESULT_OK) { 
       // Do your stuff here 
      } 
     } 
    } 

// Your listener for starting another activity, use in Main Activity 
Intent intent = new Intent(this, Activity1.class);  
startActivityForResult(intent,1); 

//Activity1 
    // start and exit, if you wanna handle Activity2 from Activity1 you need to override onActivityResult for it 
    Intent intent = new Intent(this, Activity2.class); 
    Intent goingBack= new Intent(); 
    setResult(RESULT_OK,goingBack); 
    startActivityForResult(intent,2); 
    finish(); 


//Activity2 
    //exit listener 
    Intent goingBack= new Intent(); 
    setResult(RESULT_OK,goingBack); 
    finish(); 
Verwandte Themen