android - 在不启动 Activity 的情况下传递包

标签 android arraylist bundle

我需要将一个 ArrayList 传递给一个 Activity ,这样我就可以把它列在那里,但我不想马上开始那个 Activity 。我需要在另一个“显示扫描列表”按钮按下时启动列表 Activity 。因为我想放置 bundle 的按钮只包含一个 finish(),当我准备好列表时,我用它返回到我的主菜单,然后从那个菜单我进入显示列表按钮。它开始另一项 Activity ,我知道如何传递 bundle 的唯一方法是 Intent 。但这会再次启动菜单 Activity 。我希望有人能理解这个问题,我在这里可能需要一些帮助。

谢谢前面的。

--编辑--

package org.example.sudoku;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;



public class Sudoku extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

     // Set up click listeners for all the buttons

        View exitButton = findViewById(R.id.exit_button);
        exitButton.setOnClickListener(this);
        View scanButton = findViewById(R.id.scan_button);
        scanButton.setOnClickListener(this);
        View editButton = findViewById(R.id.about_button);
        editButton.setOnClickListener(this);

    }
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.scan_button:
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "ONE_D_MODE");
            startActivityForResult(intent, 0);
        break;
        case R.id.about_button:
            ArrayList<String> scanList = new ArrayList<String>();
            scanList.add("asd");
            scanList.add("asd2");
            scanList.add("asd3");
            //String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone"};
            Intent about = new Intent(this.getApplicationContext(),About.class);
            Bundle b = new Bundle();

            b.putStringArrayList("key", scanList);


            about.putExtras(b);
            startActivityForResult(about, 0);
        break;
        case R.id.exit_button:
            finish();
        break;


                    }
                }



    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                // Handle successful scan
                Intent result = new Intent(this.getApplicationContext(),Result.class);
                Bundle b = new Bundle();
                b.putString("content",contents);
                result.putExtras(b);
                startActivityForResult(result, 0);



            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

}

这是我的主要 Activity 。我不知道我是否理解正确,但我只需要另一个 public void onActivityResult(int requestCode, int resultCode, Intent intent) 来处理由 B Activity 调用的已完成的 C Activity ?而 A 会知道标识符是 B Activity 的原因吗? 我希望我明白了。

最佳答案

因此,如果我理解了您的问题,您会遇到以下情况。您有 Activity A,您从中启动了 Activity B(在其中生成所需的 ArrayList),然后完成() Activity B,并从 Activity A 开始使用生成的列表启动 Activity C。 如果这是您的场景,最好的方法是使用 Activity 结果。在 Activity B 中调用 http://developer.android.com/reference/android/app/Activity.html#setResult(int , android.content.Intent) 它接受 bundle 并将 ArrayList 传递回 Activity A。然后当您启动 Activity C 时传递它。

您必须通过调用 http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent 来启动 Activity B , int) 并在 onActivityResult() 中处理结果

关于android - 在不启动 Activity 的情况下传递包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5434460/

相关文章:

java - 从 ArrayList 中删除对象

ruby - bundle 安装(LoadError)

java - 无法在android中使用Retrofit和Java创建POJO

java - 如何使用 Java 8 增强 List 方法中的重复对象?列表中的对象是嵌套的,这就是使这个复杂的原因

java - AsyncHttpClient 中的 ArrayList iterator.remove() 抛出 IllegalStateException

c# - .NET Core2.0 bundleconfig.json 不工作

c# - 使用 ASP.NET CSS 包时浏览器的奇怪缓存行为

java - 如果第一个 url 不可能,如何读取另一个远程 url android

android - 如何在android中显示结合数学公式的文本?

android - 淡出当前 Activity 的 View