startactivityforresult from dialogpreference (non activity)

好久不见. 提交于 2019-12-20 05:51:31

问题


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:


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.



来源:https://stackoverflow.com/questions/11037826/startactivityforresult-from-dialogpreference-non-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!