Java Question Possible to sub-class Application in B4A?

warwound

Expert
Licensed User
Longtime User
I've been wondering how to save some library state on orientation change and found a solution posted on Stackoverflow: Android: How to declare global variables? - Stack Overflow.

I've read before about sub-classing the Application class to create what amounts to a global.
I could probably get the state from my library in Activity_Pause and save it to a B4A Process_Globals but wanted to experiment with the sub-classing of the Application class anyway.

So i created my sub-class:

B4X:
package com.blah.blah;

import java.util.ArrayList;

import android.app.Application;
import anywheresoftware.b4a.BA.Hide;

@Hide
public class MyLibraryState extends Application {

   //   http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables

   protected ArrayList<String> mValues = new ArrayList<String>();
}

And updated my manifest:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
SetApplicationAttribute(android:name, ".MyLibraryState")

And my application force closes as soon as i try to run it!
The manifest file has been updated correctly:

B4X:
   <application
      android:icon="@drawable/icon"
      android:label="Oregon Jobs"
      android:name=".MyLibraryState">

I tried updating the manifest both with and with the dot in front of MyLibraryState but all that happens is a force close.

Is there any reason specific to B4A why i can't do this?
Or is it possible and i just need to experiment further?

Martin.
 

warwound

Expert
Licensed User
Longtime User
A static class member in an Activity object will retain it's values even after an orientation change?

I've found a different solution by modifying the previous code i posted:

B4X:
@Author("Martin the Verbose")
@ShortName("MyLibraryState")
public class MyLibraryState {
 private ArrayList<String> mvalues;

 // added setter and getter methods here

}

It no longer extends Application and is a data only non-Activity object that i can keep a reference to in Process_Globals.

Martin.
 

warwound

Expert
Licensed User
Longtime User
I'm currently on page 252 of Sams Teach Yourself Java In 24 Hours - still got a lot to learn i think.

Presumably a static class member is of no use if you have more than one instance of that class and each instance doesn't share the same values?

Martin.
 

warwound

Expert
Licensed User
Longtime User
But what if the library is an activity object that the user may have two or more instances of in an activity?

Let's say i want i update my TouchImageView to auto save the image position and scale on orientation change.
The user has two instances of TouchImageView in a single Activity.

Using class members in TouchImageView to save unique states wouldn't be possible - i could have a collection of states (Array or List) as a class member.
But when the instances of TouchImageView are recreated i'd have no way to know which class member saved state (from the Array or List) to apply to which new instance of TouchImageView.

It's no problem - i think i'll create a data only non-activity class to save state values to and expose that to the IDE so the user can get and set instance state on orientation change.

Martin.
 

HotShoe

Well-Known Member
Licensed User
Longtime User
If I understand the question correctly, it shouldn't matter how many instances of an object are declared. Each declaration (Dim) reserves its own memory whether it's static or not. Just as you can declare more than one edittext without having to worry about other instances of edittext interfering with each other.

--- Jem
 

HotShoe

Well-Known Member
Licensed User
Longtime User
I didn't realize that java worked in this manner too. Now I see why you are concerned, and I need to go back to learning java before commenting... :)

--- Jem
 
Top