【问题标题】:Back to the previous activity on back pressed按下后返回上一个活动
【发布时间】:2017-02-20 02:18:32
【问题描述】:

我有一个显示 FileDialog 和 Intent 以使用蓝牙的功能。 但是当我按下后退按钮时,它会进入上一个活动,它是可见的但不可点击(如屏幕截图),我必须再次按下后退按钮。 我尝试了函数onBackPressed() { finish(); },但没有正常工作。

MainActivity:

    ...
        if(item == shareMenu) {
            startActivity(new Intent(getBaseContext(), ShareViaBluetoothActivity.class));
        }
...

ShareViaBluetoothActivity:

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.util.List;

public class ShareViaBluetoothActivity extends Activity {

    private static final int DISCOVER_DURATION = 300;
    private static final int REQUEST_BLU = 1;

    private FileDialog fileDialog;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         File mPath = new File(Environment.getExternalStorageDirectory(), "//DIR//");
        fileDialog = new FileDialog(this, mPath);
        fileDialog.addFileListener(new FileDialog.FileSelectedListener() {
            public void fileSelected(File file) {
                Log.d(getClass().getName(), "selected file " + file.toString());
                setFile(file);
                sendViaBluetooth();
            }
        });
        fileDialog.showDialog();
    }

    public void sendViaBluetooth() {

        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        if(btAdapter == null) {
            Toast.makeText(this, "Bluetooth is not supported on this device!", Toast.LENGTH_LONG).show();
        } else {
            enableBluetooth();
        }
    }

    public void enableBluetooth() {

        Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

        discoveryIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVER_DURATION);

        startActivityForResult(discoveryIntent, REQUEST_BLU);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode == DISCOVER_DURATION && requestCode == REQUEST_BLU) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("*/*");

            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(file.toString())));
            intent.setPackage("com.android.bluetooth");

            PackageManager pm = getPackageManager();
            List<ResolveInfo> appsList = pm.queryIntentActivities(intent, 0);

            if(appsList.size() > 0) {
                String packageName = null;
                String className = null;
                boolean found = false;

                for(ResolveInfo info : appsList) {
                    packageName = info.activityInfo.packageName;
                    if(packageName.equals("com.android.bluetooth")) {
                        className = info.activityInfo.name;
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    Toast.makeText(this, "Bluetooth havn't been found",
                            Toast.LENGTH_LONG).show();
                } else {
                    intent.setClassName(packageName, className);
                    startActivity(intent);
                }
            }
        } else {
            Toast.makeText(this, "Bluetooth is cancelled", Toast.LENGTH_LONG)
                    .show();
        }
    }
}

【问题讨论】:

  • 您能解释一下But when I press back button, it comes to previous activity, it is visible but not clickable (like screen) and I have to press the back button once again 的确切含义吗?
  • 当我从 MainActivity 启动活动时,会出现 fileDialog。然后当我想取消时,我按下“后按按钮”。然后对话框消失,MainActivity 可见,但不可点击。我的意思是,我可以在任何我想要的地方点击屏幕,但什么也没有发生。它像截图,像照片。当我再次按下“后按按钮”时,一切正常。
  • 是的,但是如何解决呢?我尝试了finish()方法,但它不起作用。
  • back 事件被FileDialog 消费,ShareViaBluetoothActivity 实际上还没有完成。在 FileDialog 的 OnDismiss 中强制完成 ShareViaBluetoothActivity

标签: android android-activity onbackpressed


【解决方案1】:

你描述的不是那么完整。但是我这个有一些原因可能会导致这个,然后你可以检查一下。
1. 使用 super.onBackPressed() 或者给 onBackPressed() return 可以做出不同
2.请检查活动启动模式工作。

但是你最好给出更详细的代码,我可以帮你

【讨论】:

  • 我想,我付出了一切。 :) 在 MainActivity 我调用 ShareViaBluetoothActivity,并发布 ShareViaBluetoothActivity 代码。
  • ShareViaBluetoothActivity 代码贴出来是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 2020-12-29
  • 1970-01-01
相关资源
最近更新 更多