简体   繁体   English

在Android中按下后退按钮销毁数据

[英]Destroy data on pressing back button in android

I have few activity files that contains almost same code as shown below. 我的活动文件很少,几乎包含如下所示的代码。 Well i have not included onDestroy and finish() method in all of my activity files, before moving forward i want to be sure of the code posted below. 好吧,我没有在所有活动文件中都包含onDestroy和finish()方法,在继续前进之前,我想确定下面发布的代码。

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

After running the application i found a serious issue with data,it looks like android keeps data in background. 运行该应用程序后,我发现数据存在严重问题,似乎android将数据保留在后台。 How can i avoid this? 我如何避免这种情况? 在此处输入图片说明

Everytime i run app and check the data it seems to be increasing. 每次我运行应用程序并检查数据时,它似乎都在增加。

Well this is my MainActivity.java: 好吧,这是我的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();
    }
}

You will need to explicitly clear the application's user data every time you exit the aplication or at any point of time you want to during the use of your app. 每次退出应用程序时或在使用应用程序期间的任何时间点,您都需要明确清除应用程序的用户数据。

Use this: ActivityManager's clearApplicationUserData() method 使用此方法: ActivityManager的clearApplicationUserData()方法

As per documentation this will: 根据文档,这将:

Permits an application to erase its own data from disk. 允许应用程序从磁盘擦除其自身的数据。 This is equivalent to the user choosing to clear the app's data from within the device settings UI. 这等效于用户选择从设备设置UI中清除应用程序的数据。 It erases all dynamic data associated with the app -- its private data and data in its private area on external storage -- but does not remove the installed application itself, nor any OBB files. 它会删除与该应用程序关联的所有动态数据-私有数据和外部存储中私有区域中的数据-但不会删除已安装的应用程序本身,也不会删除任何OBB文件。

Give something like this a shot 试一下这样的东西

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM