tags:

views:

957

answers:

3

Having developed many desktop GUI apps (from Xt to Qt, Java Awt/Swt/Swing, etc) I really find it difficult to get used to Android.

Suppose I have the MainView Activity class which explicitly calls DetailedView via intent mechanism as shown next:

  • Since an Activity class is instantiated via onCreate() how do I customize it? (No constructor, only pass data through intent!)

  • Is there a way to get a reference for the DetailedView instance in MainActivity?

  • Is there a way to get a reference for the MainActivity instance in DetailedView?

  • How can I pass the reference to FrontEnd to the DetailedView class? Intent.putExtras() allows only for certain data types to pass to the intent receiver class.

    MainActivity {
        ...
        FrontEnd fe;
        ...
    
    
    
    public void onCreate(Bundle savedInstanceState) {
        ...
        Intent myIntent = new Intent(this, DetailedView.class);
        ...
    }
    
    
    protected void onListItemClick(ListView l, View v, int position, long id) {
        ...
        startActivityForResult(myIntent,..);
        ...
    }
    
    }
+1  A: 

I frequently cheat and use static 'getInstance' calls to communicate between Activities and views. This works as long as they're both in the same proc, and I've yet to have a data access failure...but I'm sure it's only a matter of time...IF you're looking for a hacky quick fix this could be it, otherwise you have to pass data through intents.

haseman
+1  A: 

One way of passing simple data between activities/services of a specific app is to use the SharedPreferences functionality of android.

This may not be the most elegant code to get the job done, but I routinely create a static "utility" class in my Android projects to allow for 1 line get and set of simple data types via shared preferences

private static final String PREFERENCE_FILE_NAME = "com.snctln.util.test.SharedPreferencesFile";
private static final String BOOL_VALUE_ONE = "bValueOne";

public static boolean getBooleanValue1(Context context)
{
    SharedPreferences prefs = context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
    return prefs.getBoolean(BOOL_VALUE_ONE, true); // return true if the value does not exist
}

public static void setBooleanValue1(Context context, int appWidgetId, boolean actualvalue)
{
    SharedPreferences.Editor prefs = context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE).edit();
    prefs.putBoolean(BOOL_VALUE_ONE, actualvalue);
    prefs.commit();
}
snctln
A: 

@snctln, you made my day! That's what I needed!

Johe Green