Pizzret
Pizzret

Reputation: 95

Passing extras between activities that are in tab layout?

I'm making an app that records sound and stores it somewhere on your phone. I have a tab layout and two activities in this tab layout. The first activity is recording activity and the second is library activity, where all of the recorded sounds will be stored. This is what I'm trying to do.

How the app works at the moment: After I'm done recording a dialog pops up with edittext, asking a user to rename the recorded file. After a user typed a certain name, it is added to ArrayList, I instantly called the putExtra().

fileNames = new ArrayList();
fileNames.add(newFileName);
intent.putExtra("fileNames", fileNames);

Now this is where it stops. I never had problems with passing extras between activities. But I've never done it with a tab layout before. So my question is: how do I pass extra between two activities that are under the same tab layout? I want to do this because I then want to read the arraylist in the library activity so that I can make a listview of all recorded files.

EDIT: This is the code now:

First activity:

fileNames = new ArrayList();
fileNames.add(newFileName);
getParent().getIntent().putExtra("libraryFileNames", fileNames);

Second activity:

ArrayList fileNames; //creating a global variable for arraylist (outisde of the oncreate
fileNames = getParent().getIntent().getStringArrayListExtra("libraryFileNames");

Is this the right way to approach it? The one flaw I noticed is that I'm taking the extra as "string array" while I have a normal ArrayList, not a string arraylist. Also, how could I test if this works?

Upvotes: 0

Views: 365

Answers (1)

sashomasho
sashomasho

Reputation: 353

You can use the tab activity as proxy to send data between activities. It's better idea to implement an inteface like

public interface FileNameProvider {
    public onNewFileName(String filename);
}

then from your child activity:

if (getParent() instanceof FileNameProvider)
    ((FileNameProvider) getParent()).onNewFileName(filename);

The tab activity should implement this interface and in its new method it can switch the tab (if needed) and call its child activity method that is going to handle the filename.

EDITED

ActivityOne.java

public class ActivityOne extends Activity {

public interface FileNameProvider {
    public void onNewFileName(String filename);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout1);
    findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (getParent() instanceof FileNameProvider) {
                ((FileNameProvider) getParent()).onNewFileName("some file name");
            }
        }
    });
}

}

ActivityTwo.java

public class ActivityTwo extends Activity {

public void setFileName(String filename) {
    Log.d("2", "Set filename from first activity " + filename);
}

}

MainActivity.java

public class MainActivity extends TabActivity
    implements ActivityOne.FileNameProvider {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getTabHost().addTab(getTabHost().newTabSpec("tab1")
                .setContent(new Intent(this, ActivityOne.class)));
        getTabHost().addTab(getTabHost().newTabSpec("tab2")
                .setContent(new Intent(this, ActivityTwo.class)));
    }


    @Override
    public void onNewFileName(String filename) {
        LocalActivityManager activityManager = getLocalActivityManager();
        getTabHost().setCurrentTabByTag("tab2");
        ActivityTwo two = (ActivityTwo) activityManager.getActivity("tab2");
        two.setFileName(filename);
    }
}

Upvotes: 1

Related Questions