How to prevent custom views from losing state across screen orientation changes

I think this is a much simpler version. Bundle is a built-in type which implements Parcelable public class CustomView extends View { private int stuff; // stuff @Override public Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(“superState”, super.onSaveInstanceState()); bundle.putInt(“stuff”, this.stuff); // … save stuff return bundle; } @Override public void onRestoreInstanceState(Parcelable state) { if … Read more

What’s the best manner of implementing a social activity stream? [closed]

I have created such system and I took this approach: Database table with the following columns: id, userId, type, data, time. userId is the user who generated the activity type is the type of the activity (i.e. Wrote blog post, added photo, commented on user’s photo) data is a serialized object with meta-data for the … Read more

Android Activity as a dialog

To start activity as dialog I defined it like this in AndroidManifest.xml: <activity android:theme=”@android:style/Theme.Dialog” /> Use this property inside your activity tag to avoid that your Dialog appears in the recently used apps list android:excludeFromRecents=”true” If you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog: … Read more

How to return a result (startActivityForResult) from a TabHost Activity?

Oh, god! After spending several hours and downloading the Android sources, I have finally come to a solution. If you look at the Activity class, you will see, that finish() method only sends back the result if there is a mParent property set to null. Otherwise the result is lost. public void finish() { if … Read more

How to show a dialog to confirm that the user wishes to exit an Android Activity?

In Android 2.0+ this would look like: @Override public void onBackPressed() { new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle(“Closing Activity”) .setMessage(“Are you sure you want to close this activity?”) .setPositiveButton(“Yes”, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton(“No”, null) .show(); } In earlier versions it would look like: @Override public boolean … Read more

How do I disable orientation change on Android?

Update April 2013: Don’t do this. It wasn’t a good idea in 2009 when I first answered the question and it really isn’t a good idea now. See this answer by hackbod for reasons: Avoid reloading activity with asynctask on orientation change in android Add android:configChanges=”keyboardHidden|orientation” to your AndroidManifest.xml. This tells the system what configuration … Read more

Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

I thought Activity was deprecated No. So for API Level 22 (with a minimum support for API Level 15 or 16), what exactly should I use both to host the components, and for the components themselves? Are there uses for all of these, or should I be using one or two almost exclusively? Activity is … Read more

How do I programmatically “restart” an Android app?

You can use PendingIntent to setup launching your start activity in the future and then close your application Intent mStartActivity = new Intent(context, StartActivity.class); int mPendingIntentId = 123456; PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); System.exit(0);

Start an Activity with a parameter

Put an int which is your id into the new Intent. Intent intent = new Intent(FirstActivity.this, SecondActivity.class); Bundle b = new Bundle(); b.putInt(“key”, 1); //Your id intent.putExtras(b); //Put your id to your next Intent startActivity(intent); finish(); Then grab the id in your new Activity: Bundle b = getIntent().getExtras(); int value = -1; // or other … Read more

Android Shared preferences for creating one time activity (example) [closed]

Setting values in Preference: // MY_PREFS_NAME – a static String variable like: //public static final String MY_PREFS_NAME = “MyPrefsFile”; SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString(“name”, “Elena”); editor.putInt(“idName”, 12); editor.apply(); Retrieve data from preference: SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String name = prefs.getString(“name”, “No name defined”);//”No name defined” is the default value. int idName = prefs.getInt(“idName”, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)