如何通过DialogFragment使用startActivityForResult()? [英] How to use startActivityForResult() through a DialogFragment?

查看:1995
本文介绍了如何通过DialogFragment使用startActivityForResult()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要添加一个用户名才能正常工作。 在mainActivity显示在其它检索构成数据库的顶端的用户名。 该mainActivity也有一个按钮,这会导致addusername活动,通过startActivityForResult()方法;在用户实际输入用户名则只有用户名的主要活动获取刷新并显示出来。 有一个提交了addusername活动这增加了用户名和做按钮的setResult(RESULT_OK);
一切工作正常,如果用户名通过点击addusername按钮进入。

My app requires a username to be added to function properly. The mainActivity displays the username on the top which it retrieves form the database. The mainActivity also has a button which leads to 'addusername' activity through startActivityForResult() method; when the user actually enters a username then only username on the main activity gets refreshed and displayed. There is a submit button on the 'addusername' activity which adds the username and does setResult(RESULT_OK);
Everything works fine if the username is entered by clicking the 'addusername' button.

现在,我已经加入其中,当应用程序只启动,如果没有用户名存在于数据库上被显示在mainActivity对话。对话提供了一个选项,添加用户名,而如果点击,导致addusername活动。但在pressing提交按钮,mainActivity不会被调用但用户名不刷新(不过,该数据库也得到更新)

Now, I have added a dialogue in mainActivity which gets displayed when the app starts only if no username exists on the database. The dialogue gives an option to add username, which if clicked, leads to the 'addusername' activity. But upon pressing the 'submit' button the mainActivity does get called but the username is not refreshed (though, the database does get updated)

下面是$ C $下'addusername活动:

Here is the code for 'addusername' activity:

public void submitusername(View view) {

    userdatabase name = new userdatabase(this);
    name.open();
    String user = name.getusername();
    name.close();

    EditText cinone = (EditText) findViewById(R.id.username);
    username = cinone.getText().toString();

    if(user.equals("")) {

        userdatabase entry = new userdatabase(Editprofile.this);
        entry.open();
        entry.createEntry(username, null, null, null);
        entry.close();

        Context context = getApplicationContext();
        CharSequence text = "Added new user!";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

    }

    else {

        userdatabase update = new userdatabase(Editprofile.this);
        update.open();
        update.updateUsername(username);
        update.close();

        Context context = getApplicationContext();
        CharSequence text = "Username updated!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

    }

    setResult(RESULT_OK);       
    finish();

}

下面是code的对话:

Here is the code for Dialog:

public class NouserFragment extends DialogFragment {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the Builder class for convenient dialog construction

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.nouseralert)
           .setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

                   Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);    
                   startActivity(intent);    

               }
           })
           .setNegativeButton(R.string.ignore, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
}
}

我理解这种情况发生,因为对话不调用startActivityForResult()方法。但是,即使我改变我的code,像这样的:

I understand this is happening because the Dialog does not call startActivityForResult() method. But even if I change my code, like this:

Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);    
                   startActivityForResult(intent, 0);

它仍然没有帮助。可能是因为,onActivityResult()方法不在于同一个类从startActivityForResult()被调用,但我不知道如何后得到的。

It still does not help. Probably because, onActivityResult() method does not lie in the same class from which startActivityForResult() has been called, but I don't know how to get after that.

编辑1:尝试使用

Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);

没有使用。

推荐答案

您是否尝试过这种方式:

Have you tried this way:

Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);

当你创建你的意图对象使用,而不是使用参考活动本身的BaseContext。

When you create your the Intent object you use the BaseContext, instead of using the reference to the activity itself.

这篇关于如何通过DialogFragment使用startActivityForResult()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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