2

haben 4 Aktivität: Hauptaktivität, p1, p2, p3 meine Lebenslauf-Schaltfläche funktioniert nicht. Ich möchte, wenn ich in der Schaltfläche "Lebenslauf fortsetzen" meine letzte Aktivität öffnen. zum Beispiel: wenn in p2 klicken Sie auf Haupt, dann in Haupt, wenn klicken Sie auf fortsetzen, p2 öffnen. hier ist mein Code:
Hauptaktivität:Fortsetzen-Taste funktioniert nicht, um letzte Aktivität zu gehen

public class MainActivity extends Activity { 

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

     Button resume = (Button) findViewById(R.id.resume); 
     Button next = (Button) findViewById(R.id.next); 
     Button exit = (Button) findViewById(R.id.exit); 

     resume.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 


      SharedPreferences myPref = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE); 
      String lastActivity= myPref.getString("lastactivity",""); 
      try { 
       Intent fooActivity = new Intent(MainActivity.this,Class.forName(lastActivity)); 
       startActivity(fooActivity); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } 


     }); 

     next.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MainActivity.this, p1.class); 
       startActivity(intent); 
      } 
     }); 

     exit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       moveTaskToBack(true); 
       android.os.Process.killProcess(android.os.Process.myPid()); 
       System.exit(1); 
      } 
     }); 
    } 

} 

XML-Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:text="resume" 
     android:layout_width="wrap_content" 
     android:id="@+id/resume" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:text="next" 
     android:id="@+id/next" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/exit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="exit"/> 
</LinearLayout> 

p1:

public class p1 extends Activity { 

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

     Button next = (Button) findViewById(R.id.next); 
     Button home=(Button)findViewById(R.id.home); 

     next.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(p1.this, p2.class); 
       startActivity(intent); 

      } 
     }); 

     home.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


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

      } 
     }); 

    } 
    private void storeCurrentActivity(){ 
     SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = myPref.edit(); 
     editor.putString("lastactivity", this.getClass().getSimpleName()); 
     editor.commit(); 
    } 
    @Override 
    public void onResume(){ 
     super.onResume(); 
     storeCurrentActivity(); 
    } 

} 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:text="next" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/next"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="page 1"/> 
    <Button 
     android:text="go in main" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/home"/> 

</LinearLayout> 

und p2, p3 wie p1 und dieses mein manifast:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.er.myapplication"> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 

      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".p1" 
      android:label="@string/title_activity_main2" 
      android:theme="@style/AppTheme.NoActionBar" /> 
     <activity 
      android:name=".p2" 
      android:label="@string/title_activity_p2" 
      android:theme="@style/AppTheme.NoActionBar" /> 
     <activity 
      android:name=".p3" 
      android:label="@string/title_activity_p3" 
      android:theme="@style/AppTheme.NoActionBar"/> 
    </application> 
</manifest> 
+0

Registrierst du alle Aktivitäten in 'AndroidManifest.xml'? – DysaniazzZ

+0

ich füge mein Manifast – erfan

Antwort

0

Sie unten Dinge zu tun haben, zu erreichen, was Sie

wollen
  1. , wenn Sie offen Aktivität, die Sie muss den vorherigen Aktivitätsnamen in preproance speichern.
  2. wenn Sie die Taste Resume-Taste Aktivitätsnamen von prefrance und offener Aktivität erhält mit folgendem Code

    Intent Absicht = new Intent(); intent.setClassName ("com.android.browser", "com.android.BrowserActivity"); context.startActivity (Absicht); // klar vorherige Tätigkeit vor dem Start

  3. wenn Sie App Relaunch App dann Schritt tun 2 wieder in Hauptaktivität

in dieser Art von approch Sie haben Fluss Anwendung aufrechtzuerhalten Singletone mit oder Stapel vor der Startaktivität löschen weil du Backbutton behandeln musst

+0

hinzu Ich setze das in die Fortsetzen-Taste: Intent intent = new Intent(); intent.setClassName ("com.android.browser", "com.android.BrowserActivity"); context.startActivity (Absicht); – erfan

+0

aber immer noch nicht funktioniert ... – erfan