onDestroy never called [duplicate] - android

This question already has an answer here:
android: Why onDestroy not be called when i call finish?
(1 answer)
Closed 3 months ago.
I have updated the latest Android Studio and created a new Blank Project. I added the following code to the MainActivity.java file, but onDestroy() is never called. Is there any way to get destroy event?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edevshop.destroy">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

just check it out and modify your onDestroy() method as below:
#Override
protected void onDestroy() {
Log.d("TAG", "Yay.. onDestroy called!");
super.onDestroy();
}
Now... Run App > Open LOGCAT > close App (By pressing BACK button)
You'll see a LOG.

Related

why splashactivity Blinking when mainactivity start

It's my project situation now.
MainActivity is LAUNCHER
SplashActivity called MainActivity onCreate()
When I think about it, it looks like there is no problem.
but after app starting,
The MainActivity screen is briefly visible before SplashActivity call.
Surprisingly, I did not see it on other devices, only galaxy s8.
Of course, I know it is not a general structure. But I can not understand it because I have been working normally.
white color is cold start style and splashActivity.
red color is mainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// some getIntent code
startActivityForResult(new Intent(this, SplashActivity.class), RESULTCODE_);
setInitLayout();
}
manifest
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SplashActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
minSdkVersion 21
targetSdkVersion 28
If you use style with android:windowBackground for splash activity, don't call setContentView(). Thats all!
It is not a good idea to use a splash screen that way . This should be strictly avoided.
With this approach you may also lead the problem of blank white page appears during splash launching and this what exactly happened to you !
i advice you to read this article and try to make your splash screen in the right way to avoid such behavior !
You have placed MainActivity class to be the launcher activity in your manifest. I am assuming that your splashscreen activity is called SplashActivity. If you want it to be shown before as the SplashScreen and then MainActivity to come later on, change your manifest code to be:
<activity android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
<activity android:name=".IntroActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
Then create your SplashActivity as suggested by Ismail in the link that he has provided
simply add handler for limited seconds of time and finish current activity
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//actvity transaction
}
},3000);
Use this thread code for splash activity it will better work -
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread thread = new Thread() {
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
Intent mainAct=new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainAct);
finish();
}
}
};
thread.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
and your manifest class will be look like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.diskapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".Activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.MainActivity" />
</application>
</manifest>
Use this thread code for splash activity. It will better work :
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread thread = new Thread() {
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
Intent mainAct=new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainAct);
finish();
}
}
};
thread.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}

onServiceConnected is not getting called after enabling accessibility

I have two app - MyApplication and FirstApp. There's is a button in MyApplicaton on click of which I am navigated to FirstApp.
Now, I want to read the contents of FirstApp using AccessibilityService.
My code goes like this for MyApplication-
MainActivity.java
package com.example.abc.myapplication;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,MyAccessibilityService.class);
startService(intent);
mButton = (Button) findViewById(R.id.click_me);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc.firstapp");
startActivity(launchIntent);
}
}
MyAccessibilityService.java
package com.example.aayushi.myapplication;
public class MyAccessibilityService extends AccessibilityService {
private AccessibilityServiceInfo info;
#Override
protected void onServiceConnected() {
Log.i("service connected","service");
info = new AccessibilityServiceInfo();
info.eventTypes=AccessibilityEvent.TYPES_ALL_MASK;
info.notificationTimeout=100;
info.feedbackType=AccessibilityEvent.TYPES_ALL_MASK;
info.packageNames = new String[]{"com.example.abc.firstapp"};
setServiceInfo(info);
}
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.i("onEvent----","yay");
Log.i("event-----------", event.toString());
//Log.i("source", event.ge)
}
#Override
public void onInterrupt() {
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abc.myapplication">
<uses-feature android:name="android.hardware.fingerprint"/>
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name=".MyAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
>
<meta-data android:name="android.accessibilityservice"
android:resource="#xml/accessibility_config"/>
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
accessibility_config.xml
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="#string/accessibility_service_description"
android:packageNames="com.example.abc.myapplication"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:settingsActivity="com.example.abc.myapplication.MyAccessibilityService" />
The problem is after enabling accessibility on device, onServiceConnected() is not at all getting called.
For testing, I am using Android 8.0.0 , API 26.
It looks like you're trying to start your accessibility service from within your activity. You can't do this. You must start your accessibility service from the accessibility services menu. Settings > General > Accessibility. You also most get rid of the code that attempts to start your service from within your activity.

Android MediaPlayer sound delayed on start

I was having a play with Android Studio to create a birthday card and have come to a slight problem with the audio file playing before MainActivity is even on screen. The audio file plays while the splash screen is still on the screen.
I have two Java files:
MainActivity.class
package com.example.android.happybirthday;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
MediaPlayer mySoundfile;
#Override
protected void onPause() {
super.onPause();
mySoundfile.release();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySoundfile = MediaPlayer.create(this, R.raw.music);
mySoundfile.setLooping(true);
mySoundfile.setVolume(100, 100);
mySoundfile.seekTo(0);
mySoundfile.start();
}
}
and Splash.class
package com.example.android.happybirthday;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread mythread = new Thread() {
#Override
public void run() {
try {
sleep(3000); // 3 second delay for cold start
Intent startMainApp = new Intent(getApplicationContext(), MainActivity.class); // initiate MainActivity
startActivity(startMainApp); // open MainActivity
finish(); // close this activity
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
mythread.start();
}
}
This is the AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.happybirthday">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Splash"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Could you kindly give some support?
Thanks :)
Thanks #Sergey and #You Kim, I managed to get it done via adding a delay in onStart within the MainActivity.class
you need to run the media player when the activity is started, not on the create.
try running media player on onStart() or onResume().
Called when activity resume is complete
protected void onPostResume()

Cocos2d Android Lock Screen or Leaving Game Forces Restart

I am in the final stages of a Cocos2d - Android game and I am having a problem that I can't figure out.
When I lock my tablet (Nexus 7) or when I hit the home button to leave the game, it restarts. It goes right back to the splash screen like the game is opening all over again. For your reference, here is my Main Activity and my Android Manifest:
public class MainActivity extends Activity {
//To get a static reference to context for shared preferences
private static Context context;
protected CCGLSurfaceView _glSurfaceView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//To get a static reference to context for shared preferences
MainActivity.context = getApplicationContext();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_glSurfaceView = new CCGLSurfaceView(this);
setContentView(_glSurfaceView);
}
//To get a static reference to context for shared preferences
public static Context getAppContext(){
return MainActivity.context;
}
#Override
public void onStart()
{
super.onStart();
CCDirector.sharedDirector().attachInView(_glSurfaceView);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
CCDirector.sharedDirector().setDisplayFPS(true);
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
CCScene scene = Splash.scene();
CCDirector.sharedDirector().runWithScene(scene);
}
#Override
public void onPause()
{
super.onPause();
CCDirector.sharedDirector().pause();
SoundEngine.sharedEngine().pauseSound();
}
#Override
public void onResume()
{
super.onResume();
CCDirector.sharedDirector().resume();
SoundEngine.sharedEngine().resumeSound();
}
#Override
public void onStop()
{
super.onStop();
SoundEngine.sharedEngine().pauseSound();
}
and
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bigfiresoftware.alphadefender"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/adicon"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="bigfiresoftware.alphadefender.MainActivity"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:hardwareAccelerated="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:configChanges="keyboard|keyboardHidden|orientation"
</manifest>
Any/all help is very much appreciated. Thanks.
add below line in your manifest file to your activity.
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
when you locked the screen the orientation of your screen changes.So that your activity destroys and again it creates.If you ad this line your orientation will not change.
The answers above didn't fix it but I finally found the answer. This was a tough one.
If anyone else finds this problem:
Remove the targetSdkVersion in AdnroidManifest.xml. Or change to some others, I haven't tried others but removal worked.
android:minSdkVersion="9"
android:targetSdkVersion="17" /> //git rid of this guy
add this line in your onstop() method
CCDirector.sharedDirector().end();
and you need to write an onDestroy() method to remove all the instances when you close the game.

Activity won't start a service

I m trying to start an IntentService from the main activity of y application and it won't start. I have the service in the manifest file. Here's the code:
MainActivity
public class Home extends Activity {
private LinearLayout kontejner;
IntentFilter intentFilter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
kontejner = (LinearLayout) findViewById(R.id.kontejner);
intentFilter = new IntentFilter();
startService(new Intent(getBaseContext(), HomeService.class));
}
}
Service:
public class HomeService extends IntentService {
public HomeService() {
super("HomeService");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getBaseContext(), "TEST", Toast.LENGTH_LONG).show();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salefinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Home"
android:label="#string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".HomeService" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
How can I make it work?
onHandleIntent gets called from a background thread. You can't modify the UI, or in this case, make Toast from outside the UI thread. So, I wouldn't expect anything to happen with your service.
Just try writing something out with Log.d() to see if your service is getting called.
It seams that android cached a bad version of the app - I forced closed it and started it again, and it worked...

Categories

Resources