android-lifecycle

Leaving android app with back button

痴心易碎 提交于 2019-12-18 11:57:14
问题 I want the users of my android app to leave my app when they press back at a certain activity. Can this be done? 回答1: You can try something like this (shows a dialog to confirm exit): @Override public void onBackPressed() { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit") .setMessage("Are you sure you want to exit?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {

android - There is no default constructor for ArrayAdapter

蹲街弑〆低调 提交于 2019-12-18 05:09:07
问题 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

When to save data to database, onPause() or onStop()?

元气小坏坏 提交于 2019-12-18 03:17:07
问题 I know this question has been asked a million times, I myself though that I already knew the answer and that the correct one was that the only guaranteed call is to onPause(), so you should save your data there. However, in many places of android documentation they always suggest not doing heavy work (such as writing data in database) in the onPause() method as it will delay the transition between the activities. According to Android Developer Guide in Table 1 onPause(): This method is

DialogFragment - retaining listener after screen rotation

☆樱花仙子☆ 提交于 2019-12-17 22:30:05
问题 I have a DialogFragment which creates a DatePickerDialog. I'm using a static method called newInstance to set the initial values in order to use the default empty constructor. However, how am I supposed to set the listener? After the screen rotation, when clicking in the "Done" button, the listener doesn't do anything since it does not exist. public class DatePickerFragment extends DialogFragment { public static final String ARG_YEAR = "year"; public static final String ARG_MONTH = "month";

Forcing Android to not redraw activity on orientation change

前提是你 提交于 2019-12-17 20:29:07
问题 I have been going gaga to figure this out. Although I have read a lot that on Orientation Change, Android kills an activity and starts it as a fresh one, and the only way to handle this is to save all the stuff inside onSaveInstanceState() and try to restore it inside onCreate() . But my activity does a lot and different kind of network activities at different times and if the orientation is changed when the network activity is being performed, I'll have to handle a lot of different and

IllegalStateException: Fragment already added in the tabhost fragment

人走茶凉 提交于 2019-12-17 17:38:26
问题 FATAL EXCEPTION: main Process: com.example.loan, PID: 24169 java.lang.IllegalStateException: Fragment already added: FormFragment{428f10c8 #1 id=0x7f050055 form} at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1192) at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:722) at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1533) at android.support.v4.app.FragmentManagerImpl$2.run(FragmentManager.java

How to use onResume()?

我的梦境 提交于 2019-12-17 15:29:29
问题 Can anyone give me an example that uses onResume() in Android? Also, if I want to restart the activity at the end of the execution of another, which method is executed— onCreate() or onResume() ? And if I want to update data, how do I put it in onResume()? 回答1: Any Activity that restarts has its onResume() method executed first. To use this method, do this: @Override public void onResume(){ super.onResume(); // put your code here... } 回答2: Restarting the app will call OnCreate() . Continuing

Android - Preserve or delete files created by the application on uninstall

≯℡__Kan透↙ 提交于 2019-12-17 06:41:33
问题 I created an application which creates and stores files to sdcard. Is there a way to bind the folder with application in order to delete all files when the user runs uninstall on android device? 回答1: There's no way for your application to know that it is being uninstalled (without modifying the kernel). All files created in the data/data/your.app.package is deleted automatically upon application uninstall. I don't think anything is cleared from the sdcard. You can do a quick test and find

fragment lifecycle: when “ondestroy” and “ondestroyview” are not called?

佐手、 提交于 2019-12-17 06:13:29
问题 Imagine this scenario: I have a Fragment in a Pager . I try to switch to other apps, so that the Activity owning my pager (and my fragment) will be stopped and temporarily destroyed, eventually. So, when I come back to my Activity, the Fragment 's callbacks onCreate , oncreateview and so forth are called. But none of the Fragment's onDestroy callbacks were called before! It seems that after "onStop" the fragment is destroyed at once. Is it a normal behavior? Is it maybe because the Activity

Display fragment viewpager within a fragment

我是研究僧i 提交于 2019-12-17 02:33:45
问题 I have a fragment which contains a ViewPager. The ViewPager is associated with an adapter that contains a set of fragments. Upon loading the parent fragment, I am met with an IllegalStateException with the message: java.lang.IllegalStateException: Recursive entry to executePendingTransactions . Some research has led me to the conclusion that the system is unable display fragments within another fragment, HOWEVER there seems to be some indication that it is possible to do exactly this with the