startactivityforresult from dialogpreference (non

2019-03-05 22:36发布

I have a dialog preference with a button on it that I want to have open another activity. When that activity is complete, I want the dialog to update a textview (in the dialog) with the information that was gathered from the activity.

In other words: Preference screen --> Dialog preference --> Dialog --> Button click event --> Activity

I used to have a normal activity call the activity (Activity --> Button click --> Activity) so I could use startactivityforresult, and then call my syncgui function from "onactivityresult". Sadly, the Dialog preference is not an activity, and therefore can only use startactivity (from context), not startactivityforresult (from activity).

Is there any other way I can tell my dialog that the activity it started is done and that it can update the textview? Here are the original functions

Old parent activity:

public void onClick(View v) {
        if(v == mSimModeBrowse) {
            Intent i = new Intent("com.shared.FileChooser");
            i.putExtra("com.shared.FileChooser.EXTRA_PATH", vsbPath);
            i.putExtra("com.shared.FileChooser.EXTRA_EXTENSIONS", vsbExtensions);
            startActivityForResult(i,0);
        }
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK) {
            final String file = data.getExtras().getString("com.shared.FileChooser.EXTRA_RESULT");
            mSimModePath.setText(file);
        }
    }

from filechooser (child activity):

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        File f = new File(path + "/" + files.get(position));
        Intent i = new Intent();
        i.putExtra(EXTRA_RESULT,f.getAbsolutePath());
        setResult(RESULT_OK,i);
        finish();
    }

1条回答
啃猪蹄的小仙女
2楼-- · 2019-03-05 23:18

Have you tried using:

runOnUiThread(new Runnable() { 
    public void run() 
    { 
        Intent i = new Intent("com.shared.FileChooser");
        i.putExtra("com.shared.FileChooser.EXTRA_PATH", vsbPath);
        i.putExtra("com.shared.FileChooser.EXTRA_EXTENSIONS", vsbExtensions);

        startActivityForResult(i,0);
    } 
 }); 

inside your Dialog's onClick event? That should cause it to run on the UI thread of the Activity.

查看更多
登录 后发表回答