Android local variable get's lost when using camera intent

前端 未结 2 1068
一向
一向 2021-01-23 17:05

I\'m dealing with a random problem which related to camera usage. Before I call camera intent - I generate UUID to store file with this name. I store this UUID in private variab

2条回答
  •  庸人自扰
    2021-01-23 17:25

    I've seen this happen on the Nexus phones, and some others. If you use DDMS to watch what is going on, I bet you'll see that your process is actually being terminated and then restarted. Thus your local state is being lost. You need to persist it, since Android can basically kill your process and restart it whenever it wants if you switch to a new task (and most of the camera capture intents set the NEWTASK flag).

    If your class is an Activity you can use onSaveInstanceState() to store your filename, then read it back out of the Bundle you get in onCreate().

    If you are not an Activity you can use the SharedPreferences store as a temporary place to save the filename:

    private static void saveTempFileName(Context context, String filename) {
        SharedPreferences settings = context.getSharedPreferences("whatever", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("com.yourstuff.whatever", filename);
        editor.commit();
    }
    

提交回复
热议问题