价值观迷失在previous活动时,我打后退按钮,而不是在this.finish()被调用 [英] Values lost in previous Activity when I hit back button, but not when this.finish() is called

查看:82
本文介绍了价值观迷失在previous活动时,我打后退按钮,而不是在this.finish()被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个奇怪的问题。我有2个活动我的工作。其中一个活动显示一个ListView,其数据通过长外是抓住了我用通过在database.query WHERE子句来获取结果。另一项活动被称为ListView项被点击时,让别人的东西添加到数据库中的ListView。

So I have a weird problem. I have 2 Activities I'm working on. One of the Activities displays a ListView whose data is grabbed via a long Extra I use to fetch the results via a WHERE clause in the database.query. The other Activity is called when a ListView item is clicked, allowing someone to add something to the database for the ListView.

活动名称是 DaysActivity.java (与ListView的活动)和 DayAddActivity.java (活动允许别人加了一天,然后在 DaysActivity.java ListView中显示出来)。

The activity names are DaysActivity.java (the activity with the listview) and DayAddActivity.java (the Activity that allows someone to add a day, which then shows up in the DaysActivity.java ListView).

我遇到的问题是,当我完成() DayAddActivity.java 它可以追溯到 DaysActivity 和ListView控件仍然存在完全填充。不过,如果我打了 DayAddActivity.java 返回按钮(按钮,在我的应用程序图标的操作栏标题的左边),当它返回到DaysActivity的.java,ListView控件是空的/没有了。

The problem I'm having is, when I finish() DayAddActivity.java it goes back to DaysActivity and the ListView is still there fully populated. However, if I hit the back button in DayAddActivity.java (the button to the left of the title in the action bar with my app icon), when it goes back to DaysActivity.java, the ListView is empty/gone.

继承人的code为:

DaysActivity.java:

DaysActivity.java:

package com.gauvion.gfit;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class DaysActivity extends ListActivity {

    private DaysDataSource datasource;
    private SimpleCursorAdapter dataAdapter;
    private boolean isEditing = false;
    private Toast toast_deleted;
    private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };
    private int[] to;
    private long routineDataID;
    private String routineDataName;

    @SuppressLint("ShowToast")
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_days); 

        routineDataID = getIntent().getLongExtra("routineDataID", 0);
        routineDataName = getIntent().getStringExtra("routineDataName");
        setTitle(routineDataName);

        toast_deleted = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        datasource = new DaysDataSource(this);
        datasource.open();

        Cursor cursor = datasource.fetchAllDays(routineDataID);
        to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
        dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
        setListAdapter(dataAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_days, menu);
        return true;        
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {     
        Cursor cursor = datasource.fetchAllDays(routineDataID);
        switch (item.getItemId()) {
        case R.id.button_days_add:
            Intent startDayAdd = new Intent(this, DayAddActivity.class);
            startDayAdd.putExtra("routineDataID", routineDataID);
            this.startActivity(startDayAdd);
            return true;

        case R.id.button_days_edit:
            to = new int[] { R.id.listitem_day_edit_name, R.id.listitem_day_edit_day };
            dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day_edit, cursor, columns, to, 0);
            setListAdapter(dataAdapter);

            isEditing = true;
            invalidateOptionsMenu();
            return true;

        case R.id.button_days_edit_done:
            to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
            dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
            setListAdapter(dataAdapter);

            isEditing = false;
            invalidateOptionsMenu();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        menu.findItem(R.id.button_days_edit).setVisible(!isEditing);
        menu.findItem(R.id.button_days_edit_done).setVisible(isEditing);

        return true;
    }

    @Override
    protected void onResume() {
        datasource.open();
        Cursor cursor = datasource.fetchAllDays(routineDataID);
        dataAdapter.changeCursor(cursor);

        super.onResume();
    }

    @Override
    protected void onPause() {
        datasource.close();
        super.onPause();
    }

} 

DayAddActivity.java:

DayAddActivity.java:

package com.gauvion.gfit;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class DayAddActivity extends Activity {
    private RoutinesDataSource datasource;
    private EditText dayName;
    private Spinner dayDay;
    private Button saveButton;
    private long routineDataID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_add);
        routineDataID = getIntent().getLongExtra("routineDataID", 0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_day_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void cancelDay(View view) {
        this.finish();
    }

}

被点击 activity_day_add.xml 取消按钮时, cancelDay()函数被调用。就像我说的,当我点击取消按钮 DayAddActivity.java 里面,它可以追溯到 DaysActivity.java 和ListView控件是好的。所有的数据是存在的。不过,如果我preSS在操作栏上的后退按钮(含后退箭头和活动的标题旁我的应用程序图标的按钮)ListView控件的 DaysActivity.java 是空的/没有了,标题也是空的(因为这也是通过一个额外的字符串值产生的)。

The cancelDay() function is called when a "Cancel" button is clicked in activity_day_add.xml. Like I said, when I click the "Cancel" button inside of DayAddActivity.java, it goes back to DaysActivity.java and the ListView is fine. All data is there. However, if I press the back button in the action bar (the button containing a back arrow and my app icon beside the title of the Activity) the ListView for DaysActivity.java is empty/gone, and the title is also empty (because this is also generated through an Extra String value).

推荐答案

您游标不会由活动管理的,这可能会导致内存泄漏,甚至ANR。你应该考虑使用加载器来填充使用游标的列表。你可以在这里找到一个例如:http://developer.android.com/guide/components/loaders.html。

Your cursors are not managed by the activity, this can cause a memory leak or even ANR. You should consider using a loader to populate a list using a cursor. You can find an example here :http://developer.android.com/guide/components/loaders.html.

装载机更好地工作,因为他们不冻结的应用程序获得了连接到数据库时,他们会从你的活动到另一个,preventing内存泄漏和再利用的资源。

Loaders work better as they don't freeze the app when the connexion to the database is obtained and they will be passed from one instance of your activity to another, preventing memory leaks and reusing resources.

但同样,在你的previous帖子:<一href="http://stackoverflow.com/questions/14041362/extras-seem-to-be-lost-onresume/14041421#comment19404459_14041421">Extras似乎丢失onResume(),你应该明白的onSaveInstanceState和的onCreate之间的联系,将参数传递给你的活动的未来的自己。看看这个帖子,其中最大比分的答案:<一href="http://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate">onSaveInstanceState ()和onRestoreInstanceState()。

But again, as in your previous post : Extras seem to be lost onResume(), you should understand the link between onSaveInstanceState and onCreate to pass arguments to the future self of your activity. Look at this post, the answer with the biggest score : onSaveInstanceState () and onRestoreInstanceState ().

这篇关于价值观迷失在previous活动时,我打后退按钮,而不是在this.finish()被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆