Android.app.Activity
lastmod 2018-08-17created 2015-11-025 edits
Notes on how to use Android's android.app.Activity class.
Methods
getLauncherLargeIconDensity
- http://developer.android.com/reference/android/app/ActivityManager.html#getLauncherLargeIconDensity()
Returns the density to use.
Simple example
To launch a new activity from an activity, use the following. (The NewActivity class must be written separately)
Then you must register the activity in AndroidManifest.xml as follows.
<manifest>
<application>
...
<activity android:name="NewActivity"></activity>
...
</application>
</manifest>
When finished, close the activity as follows.
Unlocking the lock screen from an activity
Add the code below inside the onCreate() method of the activity that should dismiss the lock screen.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
How to get the top Activity
- http://blog.naver.com/bridge139/10081126303
- http://blog.naver.com/phkaa/143212084
- http://www.androidpub.com/228912
- Finding-the-top-Activity
- http://www.androidpub.com/228912
To get the currently running top screen (Top Activity), do the following.
import android.content.Context;
import android.app.ActivityManager;
import java.util.List;
// ...
ActivityManager mActivityManager = null;
mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> rt = mActivityManager.getRunningTasks(1);
String runningTask = rt.get(0).topActivity.toString();
You must also add the following permission.
startActivityForResult / onActivityResult
Lets activities send and receive information between each other.
- Normally the
startActivity()method is used to launch an activity, but in this case you must use thestartActivityForResult()method. - Run
setResult(RESULT_OK, intent);to pass the result along; when you only want to send back a signal that something was done without a result, runsetResult(RESULT_OK). - The original activity (the one that called
startActivityForResult()) overrides theonActivityResult()method and implements whatever it wants to do with the result it received.
Caveats when using from a Fragment
Occasionally, if the requestCode value in onActivityResult() shows an unexpected value such as 65627, check whether startActivityForResult() was called properly from that Activity. This mistake can occur when using a Fragment; call it with getActivity().startActivityForResult() instead of this.startActivityForResult().
See also
- android.view.View
- android.app.Fragment
- Android:ActivityLifeCycle: Explanation of the Android Activity Lifecycle.
- Android:Manifest: Explanation of AndroidManifest.xml. Includes how to lock screen orientation, etc.
Favorite site
- Activity class reference
- [Recommended] Tasks and Back Stack
- Exchanging data between Activities with Intent
References
-
Android_startActivityForResult_onActivityResult.pdf ↩