“返回”按钮关闭分享对话框和带有WebView的子活动。

3
我有一个Activity(ListView),它打开了一个包含WebView的子Activity。然后我从WebView中打开共享对话框。但是当我按“返回”按钮时,它会关闭共享对话框和子Activity。如何使“返回”按钮只关闭共享对话框而不关闭子Activity呢?
非常感谢!
public class Journal extends Activity {
    private ViewFlow viewFlow;
    int page = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.circle_layout);
             page = getIntent().getExtras().getInt("new_page");
        viewFlow = (ViewFlow) findViewById(R.id.viewflow);
        viewFlow.setAdapter(new JournalAdapter(this, page), 0);
        CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
        viewFlow.setFlowIndicator(indic);
    } 
}

public class JournalAdapter extends BaseAdapter {
...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int _id = 0, rid = 0;
        int N = num;
        String url = "http://lookuper.ru/LookuperServer/journal/URL/index.html";
        if (position == 0) {
            _id = R.id.wv1;
            rid = R.layout.webview_1;
        } 

        url = url.replace("URL", String.valueOf(num - position));

        Tools.messageToLog("  =~=  getView() :: num = " + (num + position) + "; pos = " + position + ";  URL = " + url);
        convertView = mInflater.inflate(rid, null);

        final WebView wv = (WebView) convertView.findViewById(_id);
        wv.setWebChromeClient(new WebChromeClient());
        wv.setPadding(0, 0, 0, 0);
        wv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        wv.requestFocus(View.FOCUS_DOWN|View.FOCUS_UP);
        wv.getSettings().setLightTouchEnabled(true);        

        // wv.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
        wv.getSettings().setUseWideViewPort(true);
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.addJavascriptInterface(new Object() {
            public void performClick() {
                showShare("Product", "http://product.com");
            }
        }, "share");
        wv.addJavascriptInterface(new Object() {
            public void performClick() {
                ((Activity) context).finish();
            }
        }, "closeActivity");
        wv.loadUrl(url);
        final Activity MyActivity = (Activity) context;
        final ProgressBar v = (ProgressBar) MyActivity.findViewById(R.id.ProgressBar01);
        wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                v.setProgress(progress * 100);
                if (progress == 100)
                    v.setVisibility(ProgressBar.GONE);
            }
        }); 
        
        wv.setOnTouchListener(new View.OnTouchListener() { 
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) { 
                           case MotionEvent.ACTION_DOWN: 
                           case MotionEvent.ACTION_UP: 
                               if (!v.hasFocus()) { 
                                   v.requestFocus(); 
                               } 
                               break; 
                       } 
                       return false; 
                    }
            });
        return convertView;
    }

    private void showShare(String subject, String URL) {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        Intent vk = null, tw = null, wa = null, gm = null, mms = null, mail = null, fb = null;

        List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(shareIntent, 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo resolveInfo : resInfo) {
                String packageName = resolveInfo.activityInfo.packageName;
                Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
                targetedShareIntent.setType("text/plain");
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
                if (packageName.contains("com.facebook.katana")) {
                    targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, URL);
                    targetedShareIntent.setPackage(packageName);
                    fb = targetedShareIntent;
                } else if (packageName.contains("com.vkontakte.android")) {
                    targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, URL);
                    targetedShareIntent.setPackage(packageName);
                    vk = targetedShareIntent;
                } else if (packageName.equalsIgnoreCase("com.google.android.gm")) {
                    targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, URL);
                    targetedShareIntent.setPackage(packageName);
                    gm = targetedShareIntent; 
                } else if (packageName.contains("com.whatsapp")) {
                    targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, URL);
                    targetedShareIntent.setPackage(packageName);
                    wa = targetedShareIntent;
                } else if (packageName.contains("mail")) {
                    targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, URL);
                    targetedShareIntent.setPackage(packageName);
                    mail = targetedShareIntent;
                } else if (packageName.contains("com.android.mms")) {
                    targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, URL);
                    targetedShareIntent.setPackage(packageName);
                    mms = targetedShareIntent;
                } else if (packageName.contains("com.twitter.android")) {
                    targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, URL);
                    targetedShareIntent.setPackage(packageName);
                    tw = targetedShareIntent;
                } 
            }

            Intent chooserIntent = Intent.createChooser(mms, "VK");

            Parcelable [] parcelable = new Parcelable [6];
            parcelable[0] = new LabeledIntent(vk, "com.vkontakte.android", "VK", 0);
            parcelable[1] = fb;
            parcelable[2] = tw;
            parcelable[3] = wa;
            parcelable[4] = gm;
            parcelable[5] = mail;

            Parcelable p [] = targetedShareIntents.toArray(new Parcelable[] {});
            for (int i = 0; i < p.length; i++) {
                Tools.messageToLog(" >=< PN = " + p[i].toString());
            }
            
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, parcelable);
            chooserIntent.setAction("android.intent.action.MAIN");
            
            ((Activity) context).startActivityForResult(shareIntent, 0);

            // context.startActivity(shareIntent);

        }
    }
}

XML:

cyrcle_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/ProgressBar01"
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
        android:indeterminateOnly="false"
        android:maxHeight="20dip"
        android:minHeight="20dip"
        android:progressDrawable="@android:drawable/progress_horizontal" >
    </ProgressBar>
        
    <com.postuchat.lookuper.UI.custom.viewflow.CircleFlowIndicator
        android:id="@+id/viewflowindic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingTop="10dp" />

    <com.postuchat.lookuper.UI.custom.viewflow.ViewFlow
        android:id="@+id/viewflow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ImageButton
        android:id="@+id/ib1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingTop="10dp" 
        android:src="@drawable/smiley_button"
        android:contentDescription="tests"
        />

</FrameLayout>

web_view1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="20dp" >

    <WebView
        android:id="@+id/wv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animationCache="false"
        android:soundEffectsEnabled="false" 
        />

</LinearLayout>
1个回答

1
在您的活动类中重写onKeyDown事件并执行您想要的操作。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
       // close dialoge here
     return true;
    }
    return super.onKeyDown(keyCode, event);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接