oncreate

Android onCreate onResume

一世执手 提交于 2019-11-29 09:16:41
I have a problem. When I start for the first time my android application, in the main activity both the onCreate and the onResume are called. but I want to be called only the onCreate. What can I do? According to the SDK docs what you are seeing is the intended behavior. Have a look at the flowchart in the docs for Activity - Activity Lifecycle . Programmatically you can overcome this by keeping an instance member to track whether onResume has been called before - the first time it is called, set the variable and return e.g. private boolean resumeHasRun = false; @Override protected void

Detect First Run

半腔热情 提交于 2019-11-29 08:54:02
I am trying to detect if my app has been run before, by using this code: (This is in my default Android activity) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { Log.w("activity", "first time"); setContentView(R.layout.activity_clean_weather); } else { Log.w("activity", "second time"); setContentView(R.layout.activity_clean_weather); } } When I first run the app it says first time, when I run it a second time, first time, and a third, first time.... I am using an actual Android device and I am not using the run

android - There is no default constructor for ArrayAdapter

让人想犯罪 __ 提交于 2019-11-29 07:58:49
I m making adapter to adapt my book collection to be visible in list view. Issue is solved if I add super(context, position): public BookAdapter(Context context, int position, List <Book> updatedBooksList) { super(context, position); this.context = context; this.booksList = updatedBooksList ; } However, I want to know why I need this argument (int position) and call superclass constructor? protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Also, in the broader sense why do we always(?) need to call super.onCreate like here in every onCreate? Aren't we

“System services not available to Activities before onCreate()” Error message?

荒凉一梦 提交于 2019-11-29 06:13:12
When the user hits an icon in my app, I want the app first to check if the device is connected to the internet and then do something depending on the result it receives (for know it's just popping up a dialog, informing whether the device is connected or not). So I wrote this code: public class MainActivity extends Activity { // SOME CONSTANTS WILL BE DEFINED HERE AlertDialog.Builder builder = new AlertDialog.Builder(this); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.icoMyIcon)

How to restart the onCreate function

会有一股神秘感。 提交于 2019-11-29 04:34:42
I have an application and there are certain conditions when I want my activity to be recreated or the onCreate function is needed to be called so that different functions can be performed again. Just like when the orientation of the device changes and the oncreate function is recalled or the activity is recreated, in the same way, I want my application to be restarted. Currently I am using this.onCreate(null) but I think this is not the best way.. Please give some suggestions. Thanks alot How about creating a method outside of your onCreate() that does all of the Activities work, and in your

Saving Activity State in the onPause

不想你离开。 提交于 2019-11-29 01:23:19
I have a variable that I have successfully saved and restored using onSaveInstanceState @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // the UI component values are saved here. outState.putDouble("VALUE", liter); Toast.makeText(this, "Activity state saved", Toast.LENGTH_LONG).show(); } But this only works if the activity is destroyed. I want to save the same variable by overriding the onPause() method and getting back when the activity is not not paused anymore any ideas on how to do this are greatly appreciated As you have discovered,

get activityInfo metaData in onCreate method

风流意气都作罢 提交于 2019-11-28 23:59:31
I need to retrieve a value from the AndroidManifest.xml file, stored as a meta data: <meta-data android:value="3" android:name="myInterestingValue" /> In the onCreate method, I call the following method: private Object getMetaData(String name) { try { ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA); Bundle metaData = ai.metaData; if(metaData == null) { debug("metaData is null. Unable to get meta data for " + name); } else { Object value = (Object)metaData.get(name); return value; } } catch (NameNotFoundException e) { e

Android: Set Random colour background on create

六月ゝ 毕业季﹏ 提交于 2019-11-28 23:52:10
What I want is when I load my app up it to randomly have a certain colored background from a predefined list of strings stored in a values xml file called colours. What I currently have is one colour set as the background defined through the string colour code using the gui editor in eclipse. For the life of me can't work out how to get the background to randomly pick one of the 9 strings and display it each time the activity is activated. Guidance on this would be invaluable. In colors.xml <?xml version="1.0" encoding="utf-8"?> <resources> <item name="blue" type="color">#FF33B5E5</item> <item

How to make notification resume and not recreate activity?

我的未来我决定 提交于 2019-11-28 23:04:32
I thought I had this figured out, but after some debugging on this question: How to make notification uncancellable/unremovable I just realized my activity is still getting onCreated() and onDestroyed(), in random order. My manifest for the activity: <activity android:name="***.***.***.*****" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name" android:screenOrientation="portrait" android:launchMode="singleTop" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <

Android: Where to put activity's onCreate() code in a fragment?

情到浓时终转凉″ 提交于 2019-11-28 18:50:30
I'm converting all my Activities to Fragments so that I can use them in a ViewPager. I've searched for this but I couldn't find a satisfying answer, so that's why I'm asking it here. In my Activities, I've written some code in the onCreate() method. I for example call some findViewById() s in order to link some xml-buttons to my Activity. I also make some views invisible in the onCreate() , set an OnClickListener() , fill a TextView with text and remove a Notification, all in the onCreate() method. My question is: Where should I put this code in the fragment? In the onCreate()? onCreateView()?