Change Activity After Connected to Bluetooth Device - android

I want to change activity as soon as I get connected to A device on Bluetooth,While getting the data continuously from Bluetooth.
Link- https://github.com/googlesamples/android-BluetoothChat

There's not such thing as "change" activity.
To open an Activity:
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
To close an Activity:
finish();
If you want to make Activity look like a change rather than an open/close, you'll need to remove open/close animations in your styles.xml file:
<item name="android:windowAnimationStyle">#null</item>

Related

Call activity which is set as Home activity

I have 2 activities. Inside the manifest activity 1 is set as Launcher and activity 2 is set as Home. Now i want to call the second activity from the first one to trigger the window for setting the home launcher and then the app should be selectable.
I have tried the following:
Intent selector = new Intent(this, homeActivity.class);
selector.addCategory(Intent.CATEGORY_HOME);
selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(selector);
But it seems not working.
Is this somehow possible? Do i need to change the Default activity?
Try this :
selector.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));

Android: Black Screen between Activity

When I go one activity to another activity , between the transaction a Black screen is come for some seconds. I properly finish the activity before calling startActvity().
Am using android:theme="#android:style/Theme.Translucent" theme for my activity. Even though between the activity transaction a black screen is coming
Can any one please tell me how to resolve this
Thanks in advance :)
There is no need to finish activity before calling startActivity().
Make sure that you have set content view in the onCreate of called Activity and that you are not blocking UI thread (check onCreate, onStart and onResume if you have override them).
You don't need to manage finshing your activity, this will be managed automatically when the activity is no longer in view. Just use:
startActivity(new Intent(this, MyNextActivity.class));
And use this code in whatever method you are using to navigate the activity changes.
If you make sure your window is the background of your activities you can set the window background to a color other than black:
<item name="android:windowBackground">#drawable/window_background</item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/window_background"/>
</shape>
windowBackground in Android 6 (Marshmallow)
The other option is to manage transitions, so there is no gap between the end of the first transition and the beginning of the second. However, you have not mentioned transitions.
How to remove the delay when opening an Activity with a DrawerLayout?
for disable this default animation create one style:
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
and set it as theme for your activity in the manifest:
<activity android:name=".ui.ArticlesActivity" android:theme="#style/noAnimTheme">
</activity>
Assumption :-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
// comment code here
}
If you go from activity A to B then try to comment code in OnCreate , OnResume in Activity B Like this and check what happen still black screen is coming or not.If coming then try to change theme.
If you have a finish() or FLAG_ACTIVITY_CLEAR_TASK - a blank screen may show up on pre ICS devices
To avoid this black screen you have to add one line in intent
overridePendingTransition (0, 0);
Example(kotlin):
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
overridePendingTransition (0, 0)
Example(Java):
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition (0, 0);

More than one instance of activity due to intent

For an App I am developing, I override the back button to make it act like the home button so that the state of the main activity is preserved even when the app is exited. Now, I also send a notification to the user from time to time using a service. When this notification is pressed I want to open the main activity again. I noticed though that this creates a second instance of the app, which creates major problems. I am trying to make the main activity go to the front again, without calling oncreate again like so:
Intent to launch main activity again:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This doesn't work though. I still end up with two instances of my main activity. Does anybody know how to fix this?
By the way, I already have android:launchMode="singleInstance" in my manifest.
There's a way to force the OS to create only one instance of an activity and thats using the tag launchMode in the Manifest as shown below:
<activity android:name="YourActivity"
android:launchMode="singleInstance"/>
Hope this Helps...
Regards
Try adding this flag to the intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), works for me.

Android - How to stop animation between activity changes

I have multiple different Activity in my app and I don't want any transition animation when changing between Activities. Below is the how I'm changing between Activities:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
This works great the first time I start a new Activity. There is no animation, but when I go back to an Activity that is already started it seems like the "Intent.FLAG_ACTIVITY_NO_ANIMATION" is ignored and the default animation happens.
I can't seem to figure out why this is happening.
Have you tried overridePendingTransition()?
You can set FLAG_ACTIVITY_REORDER_TO_FRONT by code and FLAG_ACTIVITY_NO_ANIMATION in manifest as below:
Create noAnimTheme in res/values/styles.xml
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
or
<style name="noAnimTheme" parent="android:Theme.NoTitleBar">
<item name="android:windowAnimationStyle">#null</item>
</style>
and use it in manifest:
<activity android:name="SecondActivity" android:theme="#style/noAnimTheme"/>
I hope it helps
I wa needing this as I had to create activities on clicking the menus.
I did the following :
I added the FLAG_ACTIVITY_NO_ANIMATION flag to the intent. It stopped the animations while creating the activity for the first time.
However the activities in the stack which were called when we click on the same menu again (probably from a different activity), it had the animation.
So I added FLAG_ACTIVITY_NO_HISTORY to clear or rather finish the activity when it starts a new activity. This caused to create a new activity (without animation) when I click on the menu once again.
add this after creating the second intent
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
when you return to the first intent, animation is disabled, worked for me though
If you're using FLAG_ACTIVITY_REORDER_TO_FRONT then you can also override onNewIntent for later startActivity calls. This will just work for bring to front states instead of first call.
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
overridePendingTransition(R.anim.whatever, R.anim.whatever);
}
Sure, you must implement this in target activity.

Android Launcher Shortcuts

I have made a simple punch in / punch out time clock application. I want to add the user the option of making a shortcut on the homescreen that will toggle the state of the app(time out / time in) but I don't want this shortcut to open up the app on the screen at all.
here is my setupShortcut()
private void setupShortcut() {
Intent shortcutIntent = new Intent(this, Toggle.class);
// shortcutIntent.setClassName(this, Toggle.class.getName());
shortcutIntent.putExtra(EXTRA_KEY, "ToggleShortcut");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ToggleShortcut");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
}
Toggle.class is an activity that toggles the state for me. In the manifest I have these settings on it
<activity android:name=".Toggle" android:exported="true" android:theme="#android:style/Theme.Translucent.NoTitleBar">
As it is now I can create a shortcut on the home screen then press it. The first time I press it it starts the Toggle activity and completes it fine, but it also opens up the TimeClock activity on the screen. If I then hit the back button I go back to the home. I can now press this shortcut and it will start the Toggle activity and not change the screen. Before I added: shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); It would open up the TimeClock activity every time. Now it is just the first time. But I want it to never show anything on the screen when the Toggle shortcut is pressed. Does anyone have any idea of how to get rid of that?
The problem with launchers is that they start Activities, whereas what you want is to start a Service so you can modify the app state in the background. It sounds like what you really want to do is make a Widget, not a launcher shortcut. You can configure the widget to have a button that sets off a Service that will toggle things in the background.
You can "more simply" make a hidden activity that will do what you want!
This activity must use transparent themes so it never shows and call finish() at the end of the onCreate() without any particular conditions.

Categories

Resources