2016-01-01 52 views
0

我有幾個活動文件包含幾乎相同的代碼,如下所示。那麼我沒有在所有的活動文件中包含onDestroy和finish()方法,在向前移動之前我想確保下面發佈的代碼。在android中按回按鈕銷燬數據

public class Three extends AppCompatActivity { 
    Button forwardB,backwardB,homeB; 
    TextView textView2,textView4,textView5; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.one); 
     //Place advertisement here 
     AdView adView = (AdView) findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder() 
      .build(); 
     adView.loadAd(adRequest); 
     // 
     //find widget by Id 
     forwardB = (Button) findViewById(R.id.forwardB); 
     backwardB = (Button) findViewById(R.id.backwardB); 
     homeB = (Button) findViewById(R.id.homeB); 
     textView2= (TextView) findViewById(R.id.textView2); 
     textView4 = (TextView) findViewById(R.id.textView4); 
     textView5 = (TextView) findViewById(R.id.textView5); 
     textView5.setText("3/50"); 
     //set text inside textView3 and textView4 
     textView2.setText("Apfel");textView4.setText("apple"); 
     //set on click listener for forward,backward and home button 
     forwardB.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent i = new Intent(getApplicationContext(), Two.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
       startActivity(i); 
       finish(); 
      } 
     }); 
     homeB.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent i = new Intent(getApplicationContext(), MainActivity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
       startActivity(i); 
       finish(); 
      } 
     }); 
     backwardB.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent i = new Intent(getApplicationContext(), Four.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
       startActivity(i); 
       finish(); 
      } 
     }); 
    } 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 
} 

運行應用程序後,我發現數據嚴重問題,它看起來像android保持數據在後臺。我怎樣才能避免這種情況? enter image description here

每當我運行應用程序並檢查它似乎在增加的數據。

嗯,這是我的MainActivity.java:

public class MainActivity extends Activity { 
    Button btnflashcards; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 

     //create widget ids that are usable forw rest of the code 
     btnflashcards = (Button) findViewById(R.id.btnflashcards); 
    } 
    //on flash card button click 
    public void findFlashCards(View v){ 
     Intent i = new Intent(this, FlashCardSelection.class); 
     startActivity(i); 
    } 
    @Override 
    public void onBackPressed() { 
     Intent intent = new Intent(Intent.ACTION_MAIN); 
     intent.addCategory(Intent.CATEGORY_HOME); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intent); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 
} 

回答

0

您將需要每次明確清除該應用程序的用戶數據,你使用你的應用程序中退出要在aplication或在任何時間點。

使用此:ActivityManager's clearApplicationUserData() method

按文檔,這將:

允許應用程序從硬盤刪除其自己的數據。這是 等同於用戶選擇從設備設置UI中清除應用的數據的 。它會清除與 應用相關的所有動態數據 - 其私有數據和私有區域中的外部 存儲 - 但不會刪除已安裝的應用程序本身,也不會刪除任何OBB文件。

0

給這樣的一個鏡頭

import java.io.File; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 

public class HelloWorld extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle *) { 
     super.onCreate(*); 
     setContentView(R.layout.main); 
    } 

    @Override 
    protected void onStop(){ 
     super.onStop(); 
    } 

    //Fires after the OnStop() state 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     try { 
     trimCache(this); 
     } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    } 

    public static void trimCache(Context context) { 
     try { 
     File dir = context.getCacheDir(); 
     if (dir != null && dir.isDirectory()) { 
      deleteDir(dir); 
     } 
     } catch (Exception e) { 
     // TODO: handle exception 
     } 
    } 

    public static boolean deleteDir(File dir) { 
     if (dir != null && dir.isDirectory()) { 
     String[] children = dir.list(); 
     for (int i = 0; i < children.length; i++) { 
      boolean success = deleteDir(new File(dir, children[i])); 
      if (!success) { 
       return false; 
      } 
     } 
     } 

     // The directory is now empty so delete it 
     return dir.delete(); 
    } 

} 
+0

不幸的是這didnt工作 –