2

I have two activities, a MainActivity and a secondary activity (e.g.: an about screen), then I have an asynctask which updates the UI on the MainActivity. This part works fine, the asynctask updates the UI by calling a method inside the MainActivity which inflates the UI and sets some values. This method also makes all UI components visible.

What doesn't work is, after going to the About screen and back to the MainActivity, the UI is completely blank. I don't understand why this stops working after navigating back from a different activity, which otherwise works fine.

Can someone please advise?

Here's how I draw the UI. This is how I update it from the thread, and it works, until I go to the about screen:

private void DisplayMainContent()
{
    Context context = Util.DataStruct.LoadContext();
    Log.d("debug", "DisplayMainContent() loaded a context " + context.toString());

    RelativeLayout parent = (RelativeLayout)((Activity)context).findViewById(R.id.action_settings);
    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = li.inflate(R.layout.activity_main, parent);

    TextView version = (TextView) v.findViewById(R.id.latestVerField);

    version.setText(Util.DataStruct.GetVal("version")); 
}
user_noname_00
  • 269
  • 3
  • 8

1 Answers1

-3

little story about vanishing data..

  1. advice:

    • do not use new activity to achieve this - do your about as dialog or dialog fragment

    nice example how to show dialog using fragment

    • don't use static - instead use singleton pattern

    Singletons preserve the conventional class approach, and don't require that you use the static keyword everywhere. They may be more demanding to implement at first, but will greatly simplify the architecture of your program. Unlike static classes, we can use singletons as parameters or objects. Also,you can use singletons with interfaces just like any other class.

  2. where i see problem:

    this line is all u need to trace yr mistake: (i think any other fragment of yr code is irrelevant to yr problem)

version.setText(Util.DataStruct.GetVal("version"));
  1. Explanation why:

    Util.DataStruct:

    • should be singleton with valid hard reference to it eg. in Applictation class or any other which life is longer as the activity u use to display data.

    are you aware of the existence of garbage collector?

    what i'm trying to point out ? why u should avoid STATIC !?

    Code(data) flow:

    1. app launched - initializes static class/variables etc
    2. your variables are feed (via async or else way)
    3. your app is closed by ANDROID OS - regardless of the reason
    4. os recreates "stack"
    5. but not yr variables - they are empty/null/defalt - not referenced by values as they shoud in normal code flow

    context:

    from where do u use yr DisplayMainContent() ? for what u need context there ?

    • context should be "anchor" for yr app methods which need it. (it's like certain security stuff - "hi this app fragment belong to me i have the right to modify and view contents - so to do any stuuf u pass nearest context u got - from fragment activity dialog widget etc")

    • if u can use getContext() eg. ("from parent") - dont use any static one

    example:

    • in fragment:
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        Context context = container.getContext();
    }
  • in adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
        Context context = parent.getContext();
}

about inflation - use :

 LayoutInflater.from(context).inflate(res,ViewGroup,attachToRoot);
  • do u use parent in inflation(in fragment doubtless u use in activity doubtful)

for @bcorso:

enter image description here

Do not use more resources than you need.

@TomaszBest sorry, but you really don't know what you're talking about: Util.DataStruct.GetVal() is calling a static method of the static class Util.DataStruct, and therefore must return a static class variable. Static class variables are singletons (only one will ever be created), and it will not get garbage collected.

An object referenced through a static member variable is strongly referenced until the class is unloaded. A normal ClassLoader never unloads a class, but those used by application do under the right conditions.

If the static field is changed to reference a different object, the original object pointed to by the static field is eligible for GC just like any other object!

The initialization of static variables is covered in Section 2.11 Static Initializers of suns JVM spec. The specification does not define the implementation of Garbage collection - garbage collection rules for static objects will vary depending on your VM.

in sum:

If your class is holding onto this object permanently, it will only be released when the vm exits. Only Classes and interfaces loaded by the bootstrap loader may not be unloaded.

ceph3us
  • 7,326
  • 3
  • 36
  • 43
  • 1
    While this might be your coding style of choice, it doesn't answer the question. At the very least, explain why you think it is a bad idea to use a new Activity in the OP's situation. – bcorso Aug 01 '15 at 05:15
  • @bcorso it's not mine coding style it's expected - how many people are building new "second" hous next to first one only to use bathroom ? or u build second bathroom in the first one ? - it's waste of resources ! – ceph3us Aug 01 '15 at 15:49
  • It's not expected. There are many valid coding styles in Android including using no Fragments at all (The guys at Square do this), having single Activity and many fragments, or having multiple Activities and multiple Fragments. – bcorso Aug 01 '15 at 16:02
  • Also, how does setting a TextView's text to a static variable cause a memory leak? And how would moving all of this into a Fragment fix a memory leak anyway? – bcorso Aug 01 '15 at 16:08
  • @bcorso did i written about memory leaks somewhere? – ceph3us Aug 01 '15 at 16:13
  • 1
    Not an answer to question, you pointed `Util.DataStruct.GetVal("version")` while I see something more suspicious `Util.DataStruct.LoadContext()`. Context loaded from static, but without knowing how does it work under the hood it is still just suspicious. And by the way more people now think that fragments are bad idea. Read that article: https://corner.squareup.com/2014/10/advocating-against-android-fragments.html – jakubbialkowski Aug 01 '15 at 16:15
  • @TomaszBest yes, your first sentence: "you're leaking data!!" A leak of data is a memory leak... – bcorso Aug 01 '15 at 16:28
  • @bcorso leaking data != leaking memory – ceph3us Aug 01 '15 at 16:32
  • @TomaszBest if that's the case then include a definition of what you mean by "data leak" in your answer. – bcorso Aug 01 '15 at 16:36
  • @bcorso i did - read careful - about life of his data - is he aware of garbage collector ? – ceph3us Aug 01 '15 at 16:42
  • @TomaszBest sorry, but you really don't know what you're talking about: `Util.DataStruct.GetVal()` is calling a static method of the static class `Util.DataStruct`, and therefore must return a static class variable. Static class variables are singletons (only one will ever be created), and it will not get garbage collected. – bcorso Aug 01 '15 at 16:59
  • @TomaszBest okay, I'll do that. You should too: http://stackoverflow.com/a/453061/1290264 – bcorso Aug 01 '15 at 19:01
  • @TomaszBest and all, thank you for all the advice! And BTW, I need to supply DisplayMainContent() a context so I can inflate the UI. I am going to re-write my app to use fragments and use dialogs when they (activities or fragments) are not necessary. – user_noname_00 Aug 02 '15 at 04:27
  • @user_noname_00 if u show me place where u want to gain context then I can point you out how. – ceph3us Aug 02 '15 at 12:08