Android: Reusing same View object in different Activities (the case is about ad banners)

后端 未结 5 1566

I want to reuse the same view object (not the view class, or the xml file, I mean the object in the memory) in different Activities.

I almost had this done. The thin

5条回答
  •  时光取名叫无心
    2021-01-08 02:06

    If you want a variable to be used within multiple activities then best practice for it is to put them in a separate class (can be named as Constants or MyVars) as static variable and use them in any activity you want as like Constants.SavedVar or MyVars.SavedVar as below is code example.

    public class MyStaticVars {
    
    public static Context myContext;
    }
    
    // First Activity where you want to save a specific context
    
    MyStaticVars.myContext = ContextToBeSaved;
    
    // Any Other Activity where you want to reuse that context
    
    priviousContext = MyStaticVars.myContext;
    

提交回复
热议问题