繁体   English   中英

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

[英]Destroy data on pressing back button in android

我的活动文件很少,几乎包含如下所示的代码。 好吧,我没有在所有活动文件中都包含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();
    }
}

运行该应用程序后,我发现数据存在严重问题,似乎android将数据保留在后台。 我如何避免这种情况? 在此处输入图片说明

每次我运行应用程序并检查数据时,它似乎都在增加。

好吧,这是我的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();
    }
}

每次退出应用程序时或在使用应用程序期间的任何时间点,您都需要明确清除应用程序的用户数据。

使用此方法: ActivityManager的clearApplicationUserData()方法

根据文档,这将:

允许应用程序从磁盘擦除其自身的数据。 这等效于用户选择从设备设置UI中清除应用程序的数据。 它会删除与该应用程序关联的所有动态数据-私有数据和外部存储中私有区域中的数据-但不会删除已安装的应用程序本身,也不会删除任何OBB文件。

试一下这样的东西

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