startactivityforresult从对话框preference(非活动)对话框、startactivityforresult、preference

2023-09-04 23:22:51 作者:爱情宣言终是谎言

我有一个对话框preference上有一个按钮,我想有开放的其他活动。当该活动完成后,我想该对话框更新一个TextView(在对话框中)与从该活动中收集的信息。

在换句话说: preference屏幕 - >对话preference - >对话框 - >按钮单击事件 - >活动

我曾经有一个正常的活动调用活动(活动 - >按钮点击 - >活动),这样我就可以用startactivityforresult,然后从onactivityresult叫我syncgui功能。可悲的是,对话框preference不是一项活动,因此只能使用startactivity(从上下文中),而不是startactivityforresult(从活动)。

有没有其他办法,我可以告诉我的对话框,它最初的活动已经完成,它可以更新的TextView?这里是原来的功能

旧父活动:

 公共无效的onClick(视图v){
        如果(V == mSimModeBrowse){
            意图I =新的意向书(com.shared.FileChooser);
            i.putExtra(com.shared.FileChooser.EXTRA_PATH,vsbPath);
            i.putExtra(com.shared.FileChooser.EXTRA_EXTENSIONS,vsbExtensions);
            startActivityForResult(ⅰ,0);
        }
    }

保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK){
            最后字符串文件= data.getExtras()的getString(com.shared.FileChooser.EXTRA_RESULT)。
            mSimModePath.setText(文件);
        }
    }
 

从文件选择器(子活动):

  @覆盖
    保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        文件f =新的文件(路径+/+ files.get(位置));
        意图I =新意图();
        i.putExtra(EXTRA_RESULT,f.getAbsolutePath());
        的setResult(RESULT_OK,我);
        完();
    }
 

解决方案

你有没有尝试过使用:

  runOnUiThread(新的Runnable(){
    公共无效的run()
    {
        意图I =新的意向书(com.shared.FileChooser);
        i.putExtra(com.shared.FileChooser.EXTRA_PATH,vsbPath);
        i.putExtra(com.shared.FileChooser.EXTRA_EXTENSIONS,vsbExtensions);

        startActivityForResult(ⅰ,0);
    }
 });
 

您对话的onClick事件里面?这应该导致其活动的UI线程上运行。

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();
    }

解决方案

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.