On Android, how do you switch activities programmatically?
This should do it for you:
Intent myIntent = new Intent(this, MyActivityName.class);
startActivity(myIntent);
You can call that from anywhere in your current activity.
How to switch between Android apps programmatically
I finally figured out how to switch between apps
programatically (preserving their state). In each app, I used 'getTaskId()' to get its task id and saved it to a file on /sdcard/.
Then, in each app, on a button click, I call
void switchTask()
{
int tid;
ActivityManager am;
am = (ActivityManager)Ctx.getSystemService( Context.ACTIVITY_SERVICE );
tid = getPkgTaskId(); // read task id of *other* app from file
am.moveTaskToFront( tid, 0, null );
}
Note: These need permissions:
- READ_EXTERNAL_STORAGE
- WRITE_EXTERNAL_STORAGE
- REORDER_TASKS
How can I configure Launcher activity programmatically in android?
Long story short, you cannot change the Activity
that is launched by default. Update: There is an alternative as described by CommonsWare in another answer.
However, there are reasonable work arounds. In your MainActivity
you can check whether the user is logged in and immediately redirect them to the LoginActivity
. That has the added benefit of automatically returning to the MainActivity
after you have logged in.
Alternatively, you can always go first to the LoginActivity
, and if the user is already logged in, send them to the MainActivity
(rewrite the Intent history to remove the return to LoginActivity
or set the noHistory flag in the manifest).
Android Switch between activities
Here you aren't ever switching to the next Activity
, just changing the layout
of the current Activity
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
instead of setContentView()
you need to use an Intent
Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);
Since you aren't actually going to the next Activity
(java file) your onClick()
isn't set.
Edit
This is what you are doing
public class MainActivity extends Activity {
Button fButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
}
This is what you should be doing. Notice the difference in the run()
function
public class MainActivity extends Activity {
Button fButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);
}
}, 5000);
}
How to change main activity in Android programmatically
check this one below
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
Change Activity's theme programmatically
As docs say you have to call setTheme
before any view output. It seems that super.onCreate()
takes part in view
processing.
So, to switch between themes dynamically you simply need to call setTheme
before super.onCreate
like this:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Change Activity in Android
There are tons of tutorials about this, also on Stackoverflow.
Reading the Docs would help too...
But here is the Solution you are searching for:
On Android, how do you switch activities programmatically?
Enable/disable an activity programmatically
Here's an example how to enable/disable an Activity:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, com.packagename.MyActivity.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(this, com.packagename.MyActivity.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Related Topics
Android Viewmodel Has No Zero Argument Constructor
How to Get Background Color from Current Theme Programmatically
Trying to Port Apache Poi to Android
Get Application Installed Date on Android
How to Detect If Changes Were Made in The Preferences
How to Avoid Restarting Activity When Orientation Changes on Android
Is Main Thread The Same as UI Thread
How to Debug on a Real Device (Using Eclipse/Adt)
Just Upgraded My Os X to Yosemite, Android Device Chooser Not Showing Device - What to Do
Android Viewpager Padding/Margin Between Page Fragments
How to Click a Button Programmatically for a Predefined Intent
How to Get The Dimensions of a Drawable in an Imageview
How to Replace The Activity's Fragment from The Fragment Itself
Sending Message to a Handler on a Dead Thread When Getting a Location from an Intentservice
Restarting a Device Programmatically