0

更新

这是我的列表视图应用程序:

在此处输入图像描述

我单击启动意图并加载我的电子邮件活动的电子邮件选项:

在此处输入图像描述

如果用户单击“返回”,则列表视图为空白:

在此处输入图像描述

这是我设置列表视图的方法:

    public class view extends ListActivity {

ImageButton searchButton;
EditText searchName;
ListView searchedListResults;
long idToPass;
String numReturned;
String email;
SimpleCursorAdapter cursorAdapter;
DBHandler getCons;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    searchedListResults = (ListView) findViewById(android.R.id.list);

    list();

    searchName = (EditText) findViewById(R.id.inputName);

    searchedListResults.setTextFilterEnabled(true);

    searchName.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            cursorAdapter.getFilter().filter(s.toString());

            searchedListResults.refreshDrawableState();

        }

    });

    getCons = new DBHandler(this, null, null);

    cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {

            getCons.open();
            return getCons.getChanges(constraint.toString());

        }

    });
    searchedListResults.setAdapter(cursorAdapter);
}

private void list() {
    DBHandler DBsearchRef = new DBHandler(this, null, null);

    DBHandler search = new DBHandler(this, null, null);

    search.open();
    Cursor cursor = search.getData();
    search.close();
    startManagingCursor(cursor);

    String[] from = new String[] { DBsearchRef.KEY_NAME,
            DBsearchRef.KEY_TEL, DBsearchRef.KEY_EMAIL,
            DBsearchRef.KEY_COMMENTS };
    int[] to = new int[] { R.id.txtNameSet, R.id.txtContactSet,
            R.id.txtEmailSet, R.id.txtCommentSet };

    cursorAdapter = new SimpleCursorAdapter(this, R.layout.searchagain,
            cursor, from, to);
    searchedListResults.setAdapter(cursorAdapter);

}

@Override
protected void onResume() {
    super.onResume();

    list();

    searchName = (EditText) findViewById(R.id.inputName);

    searchedListResults.setTextFilterEnabled(true);

    searchName.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            cursorAdapter.getFilter().filter(s.toString());

            searchedListResults.refreshDrawableState();

        }

    });

    getCons = new DBHandler(this, null, null);

    cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {

            getCons.open();
            return getCons.getChanges(constraint.toString());

        }

    });
    searchedListResults.setAdapter(cursorAdapter);
}

@Override
public void onListItemClick(ListView list, View v, int list_posistion,
        long item_id) {
    idToPass = item_id;

    idToPass = item_id;

    DBHandler num = new DBHandler(this, null, null);

    num.open();
    numReturned = num.getNum(idToPass);

    email = num.getEmail(idToPass);
    num.close();

    final CharSequence[] items = { "Call Contact", "Email Contact",
            "Edit Contact", "Add Appointment" };

    Builder alertDialogBuilder = new AlertDialog.Builder(view.this);

    alertDialogBuilder.setTitle("Contact Options:");

    alertDialogBuilder.setItems(items,
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int item) {

                    if (items[item].equals("Call Contact")) {

                        Intent makeCall = new Intent(Intent.ACTION_DIAL,
                                Uri.parse("tel:" + numReturned));
                        startActivity(makeCall);
                    }

                    else if (items[item].equals("Email Contact")) {

                        Intent emailIntent = new Intent(
                                "com.example.flybaseapp.ContactsEmail");
                        emailIntent.putExtra("passedEmailAdd", email);
                        startActivity(emailIntent);
                    } else if (items[item].equals("Edit Contact")) {

                        Intent Edit = new Intent(
                                "com.example.flybaseapp.viewEdit");
                        Edit.putExtra("passedID", idToPass);
                        startActivity(Edit);

                    }

                    else if (items[item].equals("Add Appointment")) {

                        Intent conAdd = new Intent(
                                "com.example.flybaseapp.AddAppointmentContact");
                        conAdd.putExtra("newpassedID", idToPass);
                        startActivity(conAdd);

                    }
                }

            });

    alertDialogBuilder.show();

}

是否在覆盖的 onResume 方法中设置它?

4

3 回答 3

0

Android Activity 生命周期可以在这里找到:

http://developer.android.com/reference/android/app/Activity.html

在此处输入图像描述

于 2013-03-25T20:07:13.453 回答
0

The activity lifecycle is already implementing all the stages for your Activity. Except the onCreate() which you need to define by yourself.

You call other stages in this lifecycle ONLY if you need to do something specific at those stages.

Let's take a LoginActivity as an example.


You create the Activity like this(I know... nothing new here):

public class LoginActivity extends Activity {

    public EditText usr;
    public EditText pass;
    final Context context = this;
    private TextView loginButton;
    private Boolean justCreated = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        initLoginButton();

        initEditTexts();

        addSwipeGesture();

        justCreated = true;
    }

Now let's assume that the login was validated successfully and the Activity get's to the next step in it's brief but intense lifecycle. That would be the onPause() stage. This means that the Activity will be in the "background".

What happens to the Strings that the user set as Username and Password?.. Well, if you do not save them somewhere then they'll get lost if the user navigates back to the Login screen(this means that the Activity is recreated).

In this case you'll need to @Override the onPause() method and do something about those Strings. For simplicity's sake I will use a created, public class called SavedValues where I'll save those Strings.


The SavedValues class:

public class SavedValues {

    private static String loginUser = "";
    private static String loginPassword = "";


    public static String getLoginUser() {
        return loginUser;
    }

    public static void setLoginUser(String loginUser) {
        SavedValues.loginUser = loginUser;
    }

    public static String getLoginPassword() {
        return loginPassword;
    }

    public static void setLoginPassword(String loginPassword) {
        SavedValues.loginPassword = loginPassword;
    }
}


Now... the onPause() method included in the Activity:

public class LoginActivity extends Activity {

    public EditText usr;
    public EditText pass;
    final Context context = this;
    private TextView loginButton;
    private Boolean justCreated = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        initLoginButton();

        initEditTexts();

        addSwipeGesture();

        justCreated = true;
    }

@Override
public void onPause() {
    super.onPause();

    SavedValues.setLoginUser(usr.getText().toString());
    SavedValues.setLoginPassword(pass.getText().toString());

    justCreated = false;
}   

Everything fine and dandy up to this point. Now, when the user navigates back to the LoginActivity then it will enter the onResume() stage of it's lifecycle. If we do nothing about it then our saved values will not be set.

So, we will override the onResume() method in order to retreive those values when the LoginActivity wakes up from it's slumber:

public class LoginActivity extends Activity {

    public EditText usr;
    public EditText pass;
    final Context context = this;
    private TextView loginButton;
    private Boolean justCreated = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        initLoginButton();

        initEditTexts();

        addSwipeGesture();

        justCreated = true;
    }

@Override
public void onPause() {
    super.onPause();

    SavedValues.setLoginUser(usr.getText().toString());
    SavedValues.setLoginPassword(pass.getText().toString());

    justCreated = false;
}

@Override
public void onResume() {
    super.onResume();

    if (justCreated == true) //if the user recreated the activity, restore the login values from the previous instance
    {
        usr.setText(SavedValues.getLoginUser(), TextView.BufferType.EDITABLE);
        pass.setText(SavedValues.getLoginPassword(), TextView.BufferType.EDITABLE);

        usr.setSelection(usr.getText().length());
    }
    else //if the user only left the activity clear the login values
    {
        usr.setText("", TextView.BufferType.EDITABLE);
        pass.setText("", TextView.BufferType.EDITABLE);
    }
}



So this is how you would use some of the stages in the Activity's lifecycle to your advantage. Of course, there's more to this but I think this should get you started. Please read more on the Android Dev. Site. They have a decent documentation, albeit non-linear in a confusing sort of way.


UPDATE

To answer your updated question:

You can use my SavedValues example and put into it something like this:

private static ArrayList<Custom> yourList;

public static ArrayList<Custom> getyourList() {
        return yourList;
}

public static void setyourList(ArrayList<Custom> yourList) {
        SavedValues.yourList = yourList;
}

Then in your onPause() method you write this:

@Override
public void onPause() {

    SavedValues.setyourList(getyourList()); // save the selected item list
    super.onPause();
    }

Now if you press the back button and return to your Activity the data should be there.

I have used an almost identical approach by using an Array adapter populated from a Oracle DB. Works like a charm.



Cheers and good luck!

于 2013-03-25T20:23:43.897 回答
0

Android 生命周期将在适当的时候运行必要的方法,即使它们不是在您的代码中编写的。onCreate()是您唯一需要的override。所有其他方法都将根据stateActivity的情况运行。但是,override如果需要,您可以使用任何或所有这些方法。

例如,由于onResume()每次Activity可见时都会运行但onCreate()仅在Actviity首次创建时运行,因此您可以选择放入代码onResume()以确保每次Activity可见时运行。或者也许你想在Activity被销毁时保存一个变量

@Override
public void finish()
{
    super.finish();
    // save variable to sharedprefs or wherever here
}
于 2013-03-25T20:25:09.120 回答