I'm learning about activity lifecycle. When i rotate the phone (from potrait to landscape or otherwise) i can see the activity change from resume -> pause -> stop -> create.
My question is why doen't change from resume -> pause -> stop -> restart -> start ?
Any one help me to understand? pls!
The log:
D:\PROJECTS\AndroidTraining>adb logcat System.out:I *:S
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
I/System.out(21074): -------------------- ON CREATE ------------------
I/System.out(21074): -------------------- ON START ------------------
I/System.out(21074): -------------------- ON RESUME ------------------
I/System.out(21074): -------------------- ON PAUSE ------------------
I/System.out(21074): -------------------- ON STOP ------------------
I/System.out(21074): -------------------- ON CREATE ------------------
I/System.out(21074): -------------------- ON START ------------------
I/System.out(21074): -------------------- ON RESUME ------------------
And my code here:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("-------------------- ON CREATE ------------------");
setContentView(R.layout.activity_main);
}
protected void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
}
protected void onStart(){
super.onStart();
System.out.println("-------------------- ON START ------------------");
}
protected void onResume(){
super.onResume();
System.out.println("-------------------- ON RESUME ------------------");
}
protected void onPause(){
super.onPause();
System.out.println("-------------------- ON PAUSE ------------------");
}
protected void onStop(){
super.onStop();
System.out.println("-------------------- ON STOP ------------------");
}
protected void onRestart(){
super.onRestart();
System.out.println("-------------------- ON RESTART ------------------");
}
protected void onDetroy(){
super.onStop();
System.out.println("-------------------- ON DETROY ------------------");
}
check your code mate...in
onDestroy()
method you call
super.onStop();
thats why you didn't get on Destroy in your log. put
super.onDestroy();
inside onDestroy() method
then you easily understand the activity life cycle - on orientation change.
Related
I have this button on the topmost left part. And for some reason it is not going back to it's previous page when I click it. I've checked the other links already but it is not working.
The activity code on that one
public class CardListActivity extends Activity {
private static final String LOG_TAG = CardListActivity.class.getSimpleName();
private EventBus eventBus;
private Activity activity;
private CardListRequest cardListRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
public void init() {
Log.e(LOG_TAG, "XXXX Start : init XXXX");
setUpActionBar();
activity = this;
setContentView(R.layout.activity_card_list);
Log.e(LOG_TAG, "XXXX Finish : init XXXX");
}
private void setUpActionBar() {
getActionBar().setTitle(CardListActivity.class.getSimpleName());
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
AndroidManifest.xml
...
<activity
android:name=".CardListActivity"
android:label="#string/title_activity_card_list"
android:parentActivityName=".HomeActivity2">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.pw.mccdealsapp.HomeActivity2" />
</activity>
...
This page contains all the information to create an Up button that works correctly.
You need to add, in your activity, something like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
first on your onCreate method put this
//action bar back icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
then override this method and make back opetion go to its parent
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}
My application has an Introduce activity that show process bar before using app.
pb = (ProgressBar) findViewById(R.id.pb_loader);
final Handler h = new Handler() {
#Override
public void handleMessage(Message message) {
pb.setVisibility(View.INVISIBLE);
Intent it = new Intent(FirstIntroActivity.this, SecondIntroActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
}
};
h.sendMessageDelayed(new Message(), 3000);
But after I press BACK button to exit application, my phone is turn back to application and go to SECOND activity ( after 3000ms ). How to resolve this error?
Alternative is to use a Timer to schedule start of your second activity.we can cancel starting the second activity by cancelling timer in OnBackPressed() callback.
private Timer timer;
#Override
protected void onResume() {
super.onResume();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// add code to start your second activity
}
}, 2000);
}
#Override
public void onBackPressed() {
timer.cancel();
super.onBackPressed();
}
I'm trying to run an App that has only one background song that runs on all activities. But some how when I open another activity, (all the activities are extends of the main activity), the application opens another session of the song. I tried to fix it but with no success.
I don't understand why the "Music:IsPlaying" is always false despite that the song is playing, this is my code:
public class MainActivity extends ActionBarActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Music = MediaPlayer.create(MainActivity.this, R.raw.ad_matai);
if (!Music.isPlaying())
{
Music.start();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void OpenMyProfile(View view)
{
Intent open_my_profile = new Intent(this,MyProfile.class);
startActivity(open_my_profile);
}
public void OpenPeople(View view)
{
Intent open_people = new Intent(this,PazamPeople.class);
startActivity(open_pazam_people);
}
Why don't You use service for that? If You have task which should be active for longer than single activity lifecycle it should be service for that. Hit Google with 'music service android'
Below is my code. I am having a problem when I call SensorManager.registerListener, my app will crash. Can someone tell me what's going on?
I just follw the web guide to setup SensorManger, Sensor(Accelerometer) and then register the action lintener to detect the montion of accelerometer.
I used API 21 to develop this app.
public class MainActivity extends ActionBarActivity implements SensorEventListener{
private TextView tip;
private SensorManager mSensorManager;
private Sensor mSensor;
private float axisX = 0;
private float axisY = 0 ;
private float axisZ = 0;
#Override
protected void onResume() {
super.onResume();
setUpAcceleratorSensor();
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpAcceleratorSensor();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setUpAcceleratorSensor(){
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
if((mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)) != null);
else
Toast.makeText(this, "No Sensor Device Exist", Toast.LENGTH_LONG).show();
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
Sensor mySensor = event.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
if(event.values[0] != 0 || event.values[1] != 0 || event.values[2] != 0){
axisX = event.values[0];
axisY = event.values[1];
axisZ = event.values[2];
tip.setText("Detect your montion");
}
}
else
Toast.makeText(this, "Cannot Get Sensor Device", Toast.LENGTH_LONG).show();
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
Thanks.
First that I always check when something like this goes wrong, is to check that you have all the correct permissions in the Android Manifest; however, I don't believe that there are any permissions associated with using the position sensors. I would check on this first. That is what comes to mind first, after you post logcat, we will be able to give a more detailed answer.
Try getting the sensor this way
mSensor = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); instead in your setUpAccelerometer() method.
First post here, but stackoverflow has solved soooo many problems for me. This one though, I can't seem to figure out. I'm creating an android app that de-increments and TextView value by 1 with every proximity detection. For some reason, when the Activity starts or resumes (power button), it logs 2 proximity hits. I've double-checked to ensure that I'm not close enough to the detector when pushing the power button.
Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exercise);
//start proximity
startProximitySensor(sensorListener());
}
private SensorEventListener sensorListener(){
listener = new SensorEventListener(){
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_PROXIMITY){
String maxRange = String.valueOf(mProximitySensor.getMaximumRange());
if(event.values[0] == Float.parseFloat(maxRange)){
updateTextView();
Log.i(TAG,"Proximity Sensor Reading: "+ String.valueOf(event.values[0]));
}
}
}
};
return listener;
}
private void startProximitySensor(SensorEventListener proximitySensorEventListener){
mSensorManager = (SensorManager)getSystemService(getApplicationContext().SENSOR_SERVICE);
mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if(mProximitySensor == null){
Log.i(TAG,"No proximity sensor found!");
}else{
proximityStarted = true;
Log.i(TAG,"Proximity started");
mSensorManager.registerListener(proximitySensorEventListener,mProximitySensor,SensorManager.SENSOR_DELAY_UI);
}
}
I've managed to get around it by creating a timeStamp in onResume, and comparing that to the SystemTime generated on each sensor change. If they differ for anything less than 1 second, then the TextView won't update. This works, but I'd still like to know if I'm missing something.
Thanks in advance