2

我的應用程序工作正常,但我的目的是,當我關閉應用程序,然後再次運行它,我的應用程序在最後一個活動打開。我想在我再次打開主要活動時顯示,如果我點擊了簡歷然後上次活動打開。所以,我有4 Activity稱爲Main Activity (my main)p1p2p3怎樣才能在上次活動中使用恢復按鈕?

主要活動:

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) { 


       if (!isTaskRoot() 
         && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) 
         && getIntent().getAction() != null 
         && getIntent().getAction().equals(Intent.ACTION_MAIN)) { 

        finish(); 
        return; 
       } 


      } 
     }); 

     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佈局:

<?xml version="1.0" encoding="utf-8"?> 
<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 AppCompatActivity { 

    @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); 

      } 
     }); 

     } 
    } 

P1 XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<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> 

和P2,P1那樣

例如P3:當我在P2然後點擊轉到主,主當點擊退出並重新運行我的應用程序,我的應用程序打開的P2我想當我重新運行應用程序的主要活動,如果我點擊簡歷,然後p2來顯示。

回答

2

要打開主活動時重新開張,你應該後startActivity(intent);

調用

private void FinishActivity(){ 
this.finish(); 
} 
在你的onclick事件

爲恢復您的活動商店當前活動類的字符串來共享偏好

撥打以下方法在您的代碼之前moveTaskToBack(true);

private void storeCurrentActivity(){ 
     SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF",   Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = myPref.edit(); 
     editor.clear(); 
     String packageName = this.getPackageName(); 
     String className = this.getClass().getSimpleName(); 
     editor.putString("lastactivity",packageName+"."+className); 
     editor.commit(); 
} 

,結果存儲到一個共享的偏好

,當你復出到您的應用程序只是檢查SharedPreference下面的代碼

使用您的主要活動後setContentView(R.layout.main_page);

String lastActivity= getLastActivity(); 
try { 
      Intent fooActivity = new Intent(this,Class.forName(lastActivity)) 
      startActivity(fooActivity) 
    } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 

發生這種方法在你的MainActivity

private String getLastActivity(){ 
        SharedPreferences myPref = = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE); 
        String lastActivity= myPref.getString("lastactivity",""); 

     } 

這可能會幫助你

解決方案對於這一特定問題

與下面的代碼

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

      } 
     }); 

如果你在活動P1上面的代碼將存儲您actiivty,如更換您的主頁按鈕的動作,它將存儲P1在去年的活動

也請刪除onResume行動

+0

ü可以從我的代碼解釋呢?所以我把私人無效存儲當前活動在我的主要按鈕去?和另一個代碼...只是告訴我,我必須在我的代碼中使用此代碼,請致謝 – erfan

+0

您應該在您退出應用程序時始終調用「storeCurrentActivity()」。 –

+0

這段代碼對我來說什麼都沒有 – erfan

0

我會用克利福德的重爲你提出一個解決方案,因爲你無法理解他的答案,儘管我很清楚。

上述方法,將存儲在將這個方法SharedPreferences

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(); 
} 

的活動,並呼籲他在所有的onResume在每一個活動,除了在主營:

@Override 
    public void onResume(){ 
     super.onResume(); 
     storeCurrentActivity(); 
    } 

在你的簡歷按鈕,只需致電上次保存的活動

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(this,Class.forName(lastActivity)) 
      startActivity(fooActivity) 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 
}); 
+0

謝謝回答Ismael,所以我按照你的解釋做,但仍然有兩個問題:1)恢復按鈕不起作用。 2)當重新啓動應用程序主要活動(主頁)沒有加載只是加載上次活動,當我和點擊去主。 – erfan

+0

例如,如果在p2點擊進入主界面並退出應用程序。當重新發布應用程序P2先加載不主要活動 – erfan

+0

我加你的答案 – erfan

0

因此,經過一些嘗試,這是我的一個我的問題
主要業務的nswer:

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) { 

      String lastActivity= getLastActivity(); 

      try { 
       if(!lastActivity.equals("")) 
       { 
        Toast.makeText(getApplicationContext(),"previous activity :"+lastActivity, Toast.LENGTH_SHORT).show(); 
        Intent fooActivity = new Intent(MainActivity.this,Class.forName(lastActivity)); 
        startActivity(fooActivity); 
        MainActivity.this.finish(); 

       } else { 
        Toast.makeText(getApplicationContext(),"no previous activity !", Toast.LENGTH_SHORT).show(); 
       } 
      } 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) { 

      Intent intent = new Intent(Intent.ACTION_MAIN); 
      intent.addCategory(Intent.CATEGORY_HOME); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 
     } 
    }); 
} 
private String getLastActivity(){ 
    SharedPreferences myPref = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE); 
    String lastActivity= myPref.getString("lastactivity",""); 

    return lastActivity; 
} 


} 

P1,P2,P3:

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); 
        storeCurrentActivity(); 
        startActivity(intent); 
        p1.this.finish(); 


       } 
      }); 

    } 
     private void storeCurrentActivity(){ 
      SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE); 
      SharedPreferences.Editor editor = myPref.edit(); 
      editor.clear(); 
      String packageName = this.getPackageName(); 
      String className = this.getClass().getSimpleName(); 
      editor.putString("lastactivity",packageName+"."+className); 
      editor.commit(); 
     } 


    } 
相關問題