开发者

Android Form as an Activity

I have an application with multiple activities. In the main class I open a database and create a list of the content like so:

public class MyApplication extends Activity
{
    private List<PhoneNumber> numberList;

    public void onCreate(Bundle savedInstanceState)
    {
        ...
        numberList = new ArrayList<PhoneNumber>();
        ...
        loadDatabase()
        ...
    }

    private void loadDatabase()
    {
        // File IO and parsing stuff
        addNumber(thisNumber);   // executes for each entry
    }

    public void addNumber(PhoneNumber thisNumber)
    {
        numberList.add(thisNumber);
    }
}

This works fine and I have a ListView in the main activity that is 开发者_StackOverflowpopulated with this data. Now, moving on to the next step. I am trying to launch a form as a new activity that will add an entry to this list. The activity launches fine, and I was able to build a form easily. The problem is accessing the list to add to it.

public class AddNumber extends Activity
{
    ...
    // This function is called from the done button
    // <Button android:text="Done" android:onClick="formDone"/>
    public void formDone(View v)
    {
        // findViewByIds here to get data from form. 
        // This is done correctly and I can see my data in memory

        MyApplication access = new MyApplication();
        PhoneNumber newNumber = new PhoneNumber();

        // Fill newNumber object with data from form. Done correctly. Verified

        access.addNumber(newNumber);
    }

The last line of formDone() executes correctly. Through debugging I can step through to the Application class and it executes addNumber(). Before executing numberList.add(thisNumber); I can look at the contents of memory for the thisNumber object it is trying to add to the list and that data is correct. Attempting to execute that line though crashes the application in standard Android format. (Loads View.class with 'Source not found' followed by ZygoteInit$MethodBlahBlah).

So I'm a little confused because it looks like the data being fed to addNumber() is correct, and the function works perfectly fine when the application first launches by building the list from the data in the database. Fundamentally, am I thinking about this correct? Should a form like this be it's own activity or is there a better way to do it? If it can be it's own activity, what am I doing wrong?

Thanks,

Nate


As Falmarri pointed out, it is not valid to instantiate an Application or Activity class directly.

Instead, you have two primary options for communicating between Activities.

  1. Pass information via the Intent. Add the phone number list as an extra to the intent that starts AddNumber, and return the altered list when you finish.

  2. Store information in a custom Application. Extend the Application class, (select it as your Application class in the manifest). Store your number list there. You can access the Application class from any Activity via getApplication(). Just cast it to your Application class, and access the method.


Why work this way? I think it's better to use startActivityForResult instead of just startActivity from your MyApplication class.

https://developer.android.com/reference/android/app/Activity.html

It's better to use it this way, because who knows, for memory reasons, your starting activity might get destroyed to be rebuilt afterwards and what happens to your callback then? Do not underestimate the power of intents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜