when service opens an activity, second instance of app creates - android

im working on app that require launch app from a service.
my problem is service create new instance of app Although my app is not closed. i want just one instance.
i find this question here like my problem but i can't use Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
cause my apps crashes.
i change my tabactivity(main activity) to
android:launchMode="singleTop"
but problem still remains.
would some one guid me. thank you very much
this is my service:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyAlarmService extends Service {
#Override
public void onCreate() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
openActivity(AlertActivity.class);
Log.i("LOG", "service started");
}
#Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
public void openActivity(Class activity) {
Intent i = new Intent(getBaseContext(), activity);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
}
and this is my manifest:
<application
android:icon="#drawable/drink1"
android:label="#string/app_name"
android:name=".G"
>
<activity
android:label="#string/app_name"
android:name=".MainTabActivity"
android:launchMode="singleTop" >
<!-- it cause to just one instance of app be exist -->
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlertActivity" android:theme="#style/Theme.Transparent">
</activity>
<activity android:name=".DrinkoflifeActivity"
></activity>
<activity android:name=".SettingsActivity">
</activity>
<activity android:name=".StaticActivity">
</activity>
<activity android:name=".ContactUsActivity"></activity>
<activity android:name="org.achartengine.GraphicalActivity"/>
<activity android:name=".DataActivity">
</activity>
<activity android:name=".LogoActivity"></activity>
<activity android:name=".SplashActivity">
</activity>
<service android:name=".MyAlarmService" />
</application>

Setting launchMode="singleTop" on your MainActivity is probably not necessary and isn't going to help you here.
When your service starts AlertActivity, it will create a new instance of that activity. If your application has an active task, this will also result in the active task being brought to the foreground and the new instance of AlertActivity will be created on top of any other activities in the task. In a comment you wrote that AlertActivity leads to MainActivity then the problem is in how you are starting the MainActivity from the AlertActivity.
You should probably start the MainActivity from the AlertActivity this way:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Assuming that the MainActivity is the root activity in the task and there is still an active instance (ie: it has not yet been finished), this code will remove any other activities from the task stack and go back to the existing instance of MainActivity.

Related

"Client not ready yet" when adding splashscreenactivity

I want to add a splashscreen activity to my app.
I'm using Android Studio 2.2, Preview 3
I just modify my manifest, to add in the new activity :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.youactive.hts.youactive">
<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:label="YouactiveSlidingMenu"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".SplashScreenActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
and here is my SplashScreenActivity.java
package me.youactive.hts.youactive;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashScreenActivity extends AppCompatActivity
{
private final int SPLASH_DISPLAY_LENGTH = 3000;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
}
#Override
protected void onResume()
{
super.onResume();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
// Obtain the sharedPreference, default to true if not available
boolean isSplashEnabled = sp.getBoolean("isSplashEnabled", true);
if (isSplashEnabled)
{
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
//Finish the splash activity so it can't be returned to.
SplashScreenActivity.this.finish();
// Create an Intent that will start the main activity.
Intent mainIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
SplashScreenActivity.this.startActivity(mainIntent);
}
}, 3000);
}
else
{
// if the splash is not enabled, then finish the activity immediately and go to main.
finish();
Intent mainIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
SplashScreenActivity.this.startActivity(mainIntent);
}
}
}
If I change my manifest, and put the arround MainActivity, the application launches successfull
In my Run windows,I can see this message :
adb shell am start -n "me.project.com.project/me.project.com.project.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..
I had read in some similar question in SO that adding "android:exported="true" in in Manifest could resolve this problem, but it won't in my case.
Your wifi connectivity could be reason for this error.
It might be a good idea to reconnect to your wifi before making any code related changes.

Regarding splash in Android

For now, the InitialActivity is set as the main activity in the AndroidManifest.xml file.
<activity
android:name=".ui.SplashActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.InitialActivity"
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=".ui.LoginActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.RegisterActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.MainScreenActivity"
android:launchMode="singleTop">
</activity>
After finishing the registration process in the RegisterActivity, quit the application and re-start the app, I want the app to directly go to MainScreenActivity automatically logged in. So here's the part of the InitialActivity class that checks if I am logged in and goes to the MainScreenActivity if I am.
session = new SessionManager(getApplicationContext());
// check if user is already logged in.
if(session.isLoggedIn()) {
// User is already logged in. Take him to MainScreenActivity.
Intent intent = new Intent(InitialActivity.this, MainScreenActivity.class);
startActivity(intent);
finish();
} else {
startActivity(new Intent(this, SplashActivity.class));
}
So far, I managed to go directly into the MainScreenActivity with the code above, but in this case I miss the splash. How can I modify the code so that I can still see the SplashActivity even after being automatically logged in?
FYI here's the SplashActivity class code.
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 1500);
}
}
I will use the splashActivity as the launcher activity (it will be launch every time) and I will check in this activity if user is logged or not, depending on the response launch the RegisterActivity or the mainActivity

Starting the Main activity from another activity

I am trying to achieve following case on Android, but no success:
1) Launch Application (Launcher Activity which is a subclass of Base Activity). The Base Activity has code as follows:
///This is in BaseActivity
#Override
public void onCreate(Bundle instance)
{
super.onCreate(instance);
//Config.isLoggedIn() is a static function.
if(! Config.isLoggedIn())
{
////Config.startLoginActivity is a static function
Config.startLoginActivity(this, getIntent());
finish();
}
}
The Config.startLoginActivity functions is defined as
public static void startLoginActivity(final Context ctx, final Intent finishIntent)
{
Intent i = new Intent(ctx, ItemListActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("FINISH_INTENT", finishIntent);
ctx.startActivity(i);
}
Now, the ItemListActivity contains a list of Items as {Item1, Item2, Item3}. In ItemListActivity, I am saving the passed "finishIntent" as
///This is ItemListActivity onCreate Method
if(getIntent().hasExtra("FINISH_INTENT"))
mFinishIntent = getIntent().getParcelableExtra("FINISH_INTENT");
and the onItemListSelected method is described as follows :
#Override
public void onItemSelected(String id) {
Config.setLogInState(true);
if(mFinishIntent != null)
{
Log.i("ITEMLISTACTIVITY", "Class Name = " + mFinishIntent.getClass().getName());
Log.i("ITEMLISTACTIVITY", "Starting mFinishIntent Activity");
startActivity(mFinishIntent);
finish();
}
}
But the issue is the Main Activity is not being launched again, Android takes me to the home screen instead. While looking for a solution, I saw that Google I/O app has the same implementation and that works flawlessly but in my case it is not. I am unable to figure it out. Please help.
Thanks in Advance.
Manifest File is as follows :
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.app.myapplication.ItemListActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.app.myapplication.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>
Ok Here is a quick help which works for 100 percent which I'm using not mostly but EVERYTIME! you must past it through intent and in your case here it is how it must look like.
Intent intent = new intent(//name of your activity in which you are at the moment.this, //name of activity to which you want to go.class);
startActivity(intent);
Hope this will help

Not able to change application's launching activity - Android

I am not able to change starting activity of application. In starting my starting activity was com.example.image_changer.MainActivity and with this my application run correctly. Then I change my launching activity from MainActivity to com.example.image_changer.Splash. But my application not launching com.example.image_changer.Splash activity.I want com.example.image_changer.Splash as starting activity of my application.
I have tried these solutions:
1. In eclipse, I change this setting: Run menu-->Debug Configuration---->Under my app--->Android tab--->Launch Action---->Launch(radio button)---->select(from drop down menu)--->com.example.image_changer.Splash.
2. Internet search:
I have tried all solution given on this link: Change application's starting activity
In this link zeh (user) post comment, which is possibly look like same as my problem but nobody post better solution regarding this.
Note-When I run my program then it runs splash.java but not show on emulator screen, I know this because I code System.out.println(); in thread in splash.java and it print string every time in console when I run my app.
So how to solve this problem?
This is my manifest:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/imagechanger"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.image_changer.Splash"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.image_changer.MainActivity1"></activity>
<activity android:name="com.example.image_changer.GamesFragment"></activity>
<activity android:name="com.example.image_changer.MoviesFragment"></activity>
<activity android:name="com.example.image_changer.TabsPagerAdapter"></activity>
<activity android:name="com.example.image_changer.TopRatedFragment"></activity>
<activity android:name="com.example.image_changer.Imageswipe"></activity>
<activity android:name="com.example.image_changer.Mapview"></activity>
<activity
android:name="com.example.image_changer.MainActivity"
android:label="#string/app_name"></activity>
</application>
</manifest>
This is my Splash.java class file
package com.example.image_changer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity {
/** Called when the activity is first created. */
private Thread mSplashThread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
final Splash sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(2500);
}
}
catch(InterruptedException ex){
}
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen,MainActivity.class);
System.out.println("your are in the intent");
startActivity(intent);
finish();
}
};
mSplashThread.start();
}
/**
* Processes splash screen touch events
*/
#Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}
}
Update your thread code to this.
new Handler().postDelayed(new Runnable() {
public void run() {
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen,MainActivity.class);
System.out.println("your are in the intent");
startActivity(intent);
finish();
}
}, 2500);

IntentService is not called

I want to consume an IntentService for my app to laod data from server in the background thread without intracting with or disturbing application activities.
here is my simple IntentService
public class SearchService extends IntentService {
public SearchService() {
super("SearchService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d("Tag","service started");
}
}
i start this service in the main Activity of my application
public class ChannelsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this , SearchService.class);
startService(intent);
}
I also define service attribute in manifest file but it does't work for me i don't know what is the problem
<application
<activity
android:name=".ChannelsActivity"
android:screenOrientation="landscape"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".SearchService"/>
</application>
I am stuck with this problem any help will really helpful for me.
The code you have posted should work. Do you see the debug log "service started" in the logcat window? Although it sounds stupid, the SearchService will run and immediately will shut down because there is no extensive work to do and you will not see the Service in Running Services if that is your goal.
Hope it helps.

Categories

Resources