Barta Tamás
Barta Tamás

Reputation: 31

Pass bundle whitout starting Activity

I need to pass an ArrayList to an activity, so i can list it out there , but i dont want to start that activity, right then. I need to start the list out activity on an another "Show scanned list" button push. Cause the button on that i want to put the bundle passing only contains a finish(), which i use to go back to my main menu when i got my list ready, and from that menu i go on the show list button.Which starts another activity, the only way i know how to pass a bundle is an intent. But that would start the menu Activity again. I hope some one can understand the problem, i could need some help here.

Thank you in front.

--EDIT--

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
            }
        }
    }

}

this is my main activity. I dont know if i understand right, but i just need another public void onActivityResult(int requestCode, int resultCode, Intent intent) that will handle the finished C activity, which was called by the B activity? And the A one will know that is the B activity cause of the identifier? I hope i got it.

Upvotes: 1

Views: 1553

Answers (3)

Matthew
Matthew

Reputation: 44919

The proper way to do this seems to be passing your list as an extra in a FLAG_ACTIVITY_CLEAR_TOP intent:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("list", myList);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This would happen when your user presses the button. FLAG_ACTIVITY_CLEAR_TOP will finish all of the activities that are on top of MainActivity so that you don't need to worry about calling finish(). It will also restart MainActivity, and you would read your data back in its onCreate method.

Upvotes: 0

Mojo Risin
Mojo Risin

Reputation: 8142

So if i understood the question you have the following situation. You have Activity A from which you've started Activity B (in which you generate the desired ArrayList) then you finish() activity B and from activity A you want to start activity C with the generated list. If this is your scenario the best approach will be to use activity result. In activity B call http://developer.android.com/reference/android/app/Activity.html#setResult(int, android.content.Intent) which accepts bundle and pass the ArrayList back to Activity A. Then when you're starting Activity C pass it.

You have to start the Activity B by calling http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int) and handle the result in onActivityResult()

Upvotes: 3

apesa
apesa

Reputation: 12443

You need to rethink your approach. You will not be able to "work" with an ArrayList inside an Activity w/o going through the onCreate(); It sounds like you need another activity or class that can deal with your ArrayList.

Upvotes: 0

Related Questions