Android activity added but never triggering onCreate() - android

I've added a new Activity, but the Toast message doesn't ever appear. It's not Toast specific though, it doesn't reach this class in general.
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This should appear
Toast(..., "Inside of SecondActivity onCreate", ...).show();
}
...
My AndroidManifest looks like this,
<application
...
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" ></activity>
</application>

you never call show() on your Toast
the launcher activity is MainActivity, but the Toast is inside SecondActivity. Did you add the logic to launch SecondActivity?
Edit:
From your Manifest I can read
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
So, when you execute your Application, the first activity launched will be MainActivity. Inside it you need to launch SecondActivity. For instance inside MainActivity's onCreate you could have something like:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

Related

Two instances are created when launchMode is singleTask and called with different intents

I have created a sample custom launcher to check the behavior of launchMode and intents. Here is the MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
Here is the AndroidManifest.xml contents:
<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"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="nosensor"
android:resumeWhilePausing="true"
android:taskAffinity=""
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
After installing the app when it is launched from app drawer, MainActivity starts with the intent - action as MAIN and category as LAUNCHER. Here it creates one instance. After that in Settings, if it is selected as the default launcher, MainActivity starts with the intent - action as MAIN and category as HOME. Here it creates another instance.
But if it is done in the reverse order, that is first from Settings and then from app drawer, it creates only instance.
So, why two instances are created?

App is installed twice

I have a shoppinglist App coded in Android-Studios. My app does have a splash screen. When I install the app, it is installed twice. When I uninstall one, the other one uninstalls too. I tried to delete the first intent filter on splashscreen, but then I did not have a splash screen anymore. I want my splashscreen to be remain. How to solve that? My manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projects.buylist">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The app is only one.
You have simply two activities (and then 2 icons) that can work as launcher.
If you don't want, you have to remove this part in one Activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<category android:name="android.intent.category.LAUNCHER" /> this is telling android you want the activity to be visible from the app launcher. To solve it, remove the intent-filter from MainActivity.
Delete your main intent in XML and create something like this, which will run splashscreen and then open your MainActivity
public class SplashsScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent mainIntent = new Intent(SplashsScreen.this, MainActivity.class);
SplashsScreen.this.startActivity(mainIntent);
SplashsScreen.this.finish();
}
}, 1500); // 1500 ms = 1.5 s
}
}

SEARCH_ACTION doesn't work in my SearchActivity

I'm trying to implement google's new feature, Search using a specific app. I want to ask something to google now in this format "search for ... on myApp" but it doesn't work.
This is the only code google recommend to add in AndroidManifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SearchActivity"
android:label="#string/title_activity_search"
android:parentActivityName=".MainActivity" >
<intent-filter>
<action android:name="com.google.android.gms.actions.SEARCH_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
StartActivity:
import com.google.android.gms.actions.SearchIntents;
public class SearchActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Intent i = getIntent();
if (SearchIntents.ACTION_SEARCH.equals(i.getAction())) {
String query = i.getStringExtra(SearchManager.QUERY);
Toast.makeText(this, query, Toast.LENGTH_SHORT).show();
}
}
My app isn't signed! Could it be this the problem?
Without knowing the rest of your code, I'd suggest reading up on Creating a Search Interface.
At first glance, it looks like you're missing the
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
after your IntentFilter.
ETA: I don't think having the Toast show in onCreate is going to give you any information. Searching will have to happen after the onCreate method. Breakpoint through your onCreate and see what the value of "i" (your Intent) is.

NoClassDefFoundError in SplashScreen

In my App, it opens a Splash Screen then MainActivity. I wrote the following code
SplashActivity.java
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
MainActiviy.java
here
And I added both MainActivity and SplashActivity to manifest as following:
<activity
android:name="com.emy.healthytips.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.emy.healthytips.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTop">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="com.emy.healthytips.MainActivity"
android:scheme="oauth" />
</intent-filter>
</activity>
But it gives me the following Exception
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.emy.healthytips.MainActivity
at com.emy.healthytips.SplashActivity$1.run(SplashActivity.java:20)
In this line
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
How can I fix this? Hope anyone can help me.
Thanks in advance.
Multiple things...
1) The way you are doing it defeats the purpose of a splash screen. Splash screens are supposed to give the user a pretty picture while the app loads in the background. All you are doing is adding an extra 2s delay. Take a look at this post: Android SplashScreen
2) Many people say that this method does not work on <4.0. Not sure why, but just a heads up (https://stackoverflow.com/a/5486970/2066079)
3) instead of:
startActivity(intent);
finish();
you should use:
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
You want to use the activity's version of startActivity() instead of the Runnable's. This might be unneccessary, but if it doesn't help, atleast it's good practice.
4) Also, like I mentioned in my comment, using android:name=".MainActivity" instead of android:name="com.emy.healthytips.MainActivity" in the xml is preferred to eliminate possible unchecked typo errors.

Android launch other activity before launching splash activity

I'm developing an application in android, where the app starts with a splash screen. I declared the splash activity as launcher activity in the manifest, but when I start my app, the launcher always shows up a grey activity with no content instead of the actionbar with the title of my app. The splash activity always comes up after one or two seconds.
Can anyone explain this behavior to me?
Here's my manifest:
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:logo="#drawable/navigation"
android:theme="#style/Theme.Sherlock.Light" >
<activity
android:name="de.test.basic.SplashActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity></application>
And here's the onCreate() method of the SplashActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spash_screen);
img = (ImageView) findViewById(R.id.imageView1);
img.setImageResource(R.drawable.splashscreen);
getActionbar().hide();
slideMenu.setSlidingEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this,
OtherActivity.class));
overridePendingTransition(android.R.anim.fade_in,
android.R.anim.fade_out);
finish();
}
}, SPLASH_DELAY);
}
Thanks for any help :)
Looks like its a bug in the api!
Simply add this in your starter activity declared in your manifest:
<activity
...
android:theme="#style/Theme.NoActionBar" >
<intent-filter>
...
</intent-filter>
</activity>
And if you are using ActionBarSherlock
<activity
...
android:theme="#style/Theme.Sherlock.NoActionBar" >
<intent-filter>
...
</intent-filter>
</activity>
Your question is rather similar than this one

Categories

Resources