I'm unable to perform a overridePendingTransition to start an Activity from a service class. I'm opening an activity from service class which is working perfectly but I want to start that activity with a transition.
this code is written in service class
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
activity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
but overridePendingTransition is not working its shows an error log
"android.app.Application cannot be cast to android.app.Activity"
Thanks in advance!
try using overridePendingTransition(R.anim.fade_out,R.anim.fade_in);
in your Activiy --> OnCreate()
So it will always animate irrespective of from where it is been called.
You need to call overridePendingTransition method on your activity istance, it looks like activity here is your application instance.
Related
I'm trying to start an activity from accessibility service.
So in my app, this is the one and only activity.
When I called startActivity(), the below log is printed, but will not call activity(which is named WifiAlertDialouge)'s onCreate(savedInstanceState: Bundle?).
I/ActivityManager: START u0 {flg=0x50000000 cmp=com.estimote.indoorapp/.WifiAlertDialouge} from uid 10065 on display 0
What does the above log mean? Does it mean that activity is starting?
Any guess on why my activity's onCreate is not being called?
Thanks.
If you try to start the activity from a service, you should add the FLAG_ACTIVITY_NEW_TASK flag
Intent dialogIntent = new Intent(this, WifiAlertDialouge.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
I found out that it was because I was trying to start activity inside a for loop. I found that starting activity would work if it is outside the for loop.
I have a activity which i declare as launchMode="singleInstance in Manifest file. and using blow code to starting this activity:
Intent intent = new Intent(this, LockScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
To close this activity i am doing like: finishAffinity();
But some how when i finish this activity i can see another open activity instance of this. Can someone tell me how i can finish all OR only create a single one when i need to start this activity from different places inside my application?
For an App I am developing, I want to re-launch the current activity using an intent. So I'm in MainActivity.class and I want to re-launch MainActivity.class using the following:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
This calls onDestroy() but does not re-launch the activity. Why doesn't this work?
If your are in an Activity: this.recreate();
If your are in a Fragment: getActivity.recreate();
Related Links :
How do I restart an Android Activity
how do I restart an activity in android?
Android activity restart
You could just use:
finish();
startActivity(getIntent());
Which will finish the current activity, and start a new activity with the same intent that you received when the activity was originally created. This should effectively re-launch the activity as is.
Edit:
See Reload activity in Android
Just do this:
Intent i=getIntent();//This simply returns the intent in which the current Activity is started
finish();//This would simply stop the current Activity.
startActivity(i);//This would start a new Activity.
Include this one ...
startActivity(intent);
If I call startActivityForResult and the activity that starts is also calling startActivityForResult on another activity,
is it possible that the first activity will be stopped ?
Is there a way to prevent it from happen?
What context should I pass each intent I create?
some code to figure the process
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivityForResult(intent, AbstractSettingsActivity.SETTINGS_ACTIVITY_REQUEST_CODE);
// this is inside the Settings activity
Intent intent = new Intent(getBaseContext(), SettingsTabsActivity.class);
startActivityForResult(intent, CUSTOMIZE_TAB_REQUEST_CODE);
// at this point i got ondstroy on main activity - main is not the root
In any case (either its startActivity or startActivityForResult), when you start a new activity, your current Activity will go into stopped state by raising its onStop method. Its the way Android's Activity life-cycle is designed. It has nothing to do with a type of context.
However, if you don't want to occur onStop, then perhaps you may try emulating the expected view(s) through Dialogs which will cause your Activity to reach up till its onPause state.
I Mean that i want to use one activity into another activity, Like class using create instance of that class. Is it Possible?
Well, I think you should use Intents to call an activity from another activity.
Call this from your Activity:
Intent in = new Intent(getApplicationContext(), NextActivity.class);
startActivity(in);
you can do it only by saying startActivity(), no other go. you can't make an instance of Activity because , an Activity gets created when its onCreate() method gets called, but when you say new MyActivity() its default constructor is called and not its onCreate() method (which Android OS will not accept). so always say startActivity() or startActivityForResult() which are handled by android OS
Write this code from where you want to run activity
Intent intent = new Intent(current_Activity_name.this,New_Activity_name.class);
startActivity(intent);
And add the following code into manifest file
<activity android:name=".New_activity_name" />
Well, since an Activity is a displayable-window, the appropriate concept would be that one Activity can be "launched" from another. This is how you achieve that:
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(i);
This code snippet can launch NewActivity from any point in the CurrentActivity code, for example, an 'OnClickListener'.
Yes, it is possible. This is achieved through Intents.
Intent intent = new Intent(this.getApplication(), TARGET_ACTIVITY_NAME.class);
//To add data use intent.putExtra(NAME,VALUE);
intent.setData(data.getData());
try
{
startActivity(intent); // This ll launch the TARGET_ACTIVITY_NAME
}
catch(Exception e)
{
}
For more information refer this link.
Shash