未知的空白活动?

时间:2014-04-13 20:35:28

标签: android android-layout

我正在尝试打开存储在我的资源文件夹中的pdf文件。我正在使用的代码如下。代码工作得很好,但我面临的问题是当我打开pdf后按下后退按钮我进入空白屏幕时,我必须再回去一次到达初始化pdf启动的屏幕。谁能帮我这个。感谢

public class SampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CopyReadAssets();    
    }

    private void CopyReadAssets() {
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "git.pdf");
        try {
            in = assetManager.open("git.pdf");
            out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            Log.e("tag", e.getMessage());
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/git.pdf"),
                "application/pdf");

        startActivity(intent);
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

好的,问题是你正在从另一个活动中调用这个活动 - 然后从这个活动中你再调用另一个来实际显示PDF文件。当用户按下' back'从PDF中,你最终看到了这个活动;然后你按下'返回'再次 - 结束原来的呼唤。

相反,不要创建第二个活动。将CopyReadAssetcopyFile方法移至您的第一个活动,然后点击按钮(或触发的任何内容)调用CopyReadAsset方法。

此活动无需存在。