intent setFlags FLAG_ACTIVITY_CLEAR_TOP - android

On Android, I started 4 activities A, B, C, D, if I want to go back from D to A, I can use 'intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)'. But if the activities are same class activities opened as follows, How can I go back from D to A now?
Intent i = new Intent(FlagsTest.this, FlagsTest.class);
startActivity(i);

You can start any activity you want like this to bring them to front :
To go back from D to A do something like this:
Intent i = new Intent(context, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);

Try This code
Intent intent = new Intent(this, Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Use this Tutorial It will Help U LINK

Try This way,hope this will help you to solve your problem.
1.write this code on master activity or application activity.
private KillReceiver clearActivityStack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
clearActivityStack = new KillReceiver();
registerReceiver(clearActivityStack, IntentFilter.create("clearStackActivity", "text/plain"));
// register to clear stack
}
private final class KillReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(clearActivityStack); // unregister to clear stack
}
2.write this code before start A Activity
Intent intent = new Intent("clearStackActivity");
intent.setType("text/plain");
sendBroadcast(intent);
3.start your activity
Intent intent = new Intent(this, Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Related

Creating an intent inside child activity rather than parent activity while starting an activity

What is the difference in terms of perfomance and optimization if while starting an activity i create an intent inside Child Activity(Activity to be started) rather than in parent Activity(Activity which is starting an new activity).
Example-
ChildActivity.class
public static Intent createIntent(Context context) {
return new Intent(context, ChildActivity.class);
}
ParentActivity.class
startActivity(ChildActivity.createIntent(ParentActivity.this));
Is it a good practice?Will this approach have any advantage if same activity is being started from multiple activities?
We can use that practice, so that we can start an Activity from any activities by calling a static method, we will save a lot of duplicate code. For me, I create a Navigator class, this class will store all methods which start activity:
public class Navigator {
public static final String VENUE_EXTRA = "venue_extra";
private Navigator() {
}
public static void navigateToOnboarding(Activity activity) {
Intent intent = new Intent(activity, OnboardingActivity.class);
ActivityCompat.startActivity(activity, intent, null);
}
public static void navigateToSharing(Activity activity, Venue venue) {
Intent intent = new Intent(activity, SharingActivity.class);
intent.putExtra(VENUE_EXTRA, venue);
ActivityCompat.startActivity(activity, intent, null);
}
public static void navigateToAppSetting(Activity activity) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + activity.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
activity.startActivity(intent);
}
}

Using broadcast receiver android to close app

Hi am trying to use broadcast receiver to trigger some action
Activity A will broadcast action "ACTION_EXIT" and come to Main Activity.
Whenever Main Activity receive the broadcast "ACTION_EXIT" it will close the app.
My code on Activity A (to send broadcast)
Intent broadcastIntent = new Intent(Beacon_MainActivity.this, MainActivity.class);
broadcastIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
broadcastIntent.setAction("com.package.ACTION_EXIT");
sendBroadcast(broadcastIntent);
finish();
Code on Main Activity to receive the broadcast and trigger ACTION_EXIT
private void registerReceiver(){
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_EXIT");
registerReceiver(myBroadcastReceiver, intentFilter);
}
#Override
public void onResume() {
registerReceiver();
super.onResume();
}
#Override
public void onPause() {
super.onPause();
unregisterReceiver(myBroadcastReceiver);
}
BroadcastReceiver myBroadcastReceiver =
new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive", "Logout in progress");
Toast.makeText(MainActivity.this, "what the ****", Toast.LENGTH_SHORT).show();
finish();
}
};
Don't know why it's not working, anyone can help should be much appreciate
(app should be close when Main_Activity receive the broadcast on
resume)
Your MainActivity is paused while ActivityA is running, during which time your Receiver is unregistered, and therefore not getting the broadcast. You can accomplish what you want with result forwarding instead.
In MainActivity, start LoginActivity with the startActivityForResult() method, and override the onActivityResult() method to handle the result.
public static final int REQUEST_CODE = 0;
public static final String EXTRA_EXIT = "exit";
...
Intent actLogin = new Intent(this, LoginActivity.class);
startActivityForResult(actLogin, REQUEST_CODE);
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case REQUEST_CODE:
if(data == null) {
return;
}
boolean shouldExit = data.getBooleanExtra(EXTRA_EXIT, false);
if(shouldExit) {
finish();
}
break;
...
}
}
Then, in LoginActivity, add FLAG_ACTIVITY_FORWARD_RESULT to the Intent used to start ActivityA.
Intent actA = new Intent(this, ActivityA.class);
actA.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(actA);
finish();
In ActivityA, set a boolean value to indicate whether to exit, add it to a result Intent, set that as the result, and finish the Activity.
boolean shouldExit = ...
Intent result = new Intent();
result.putExtra(MainActivity.EXTRA_EXIT, shouldExit);
setResult(Activity.RESULT_OK, result);
finish();
The result will then be delivered back to MainActivity. This can also be done with only result codes, but I prefer using Intent extras.

getStringExtra() returns null when starting activity from BroadcastReceiver

SOLVED
When trying to start AppCompatActivity from my BroadcastReceiver, passed extra parameter readed in onCreate() is null.Where can be problem?
(Android 5.1.1)
In BroadcastReceiver I'm calling my activity like this:
#Override
public void onReceive(Context context, Intent intent) {
. . .
Intent detailsActivityIntent = new Intent(context, DetailsActivity.class);
intent.putExtra(DetailsActivity.ARG_ACCOUNT_ID, accountId);
detailsActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(detailsActivityIntent);
}
BroadcastReceiver is opened from notification like this:
. . .
notificationBuilder.setContentIntent(
NotificationActionReceiver.getPendingIntent(account, getApplicationContext()));
. . .
public PendingIntent getPendingIntent(Account account, Context context) {
Intent intent = new Intent(context, NotificationActionReceiver.class);
intent.putExtra(DetailsActivity.ARG_ACCOUNT_ID, account.getAccountId());
return PendingIntent.getBroadcast(context, account.getId().intValue(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
In my onCreate I'm just opening fragment:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
String accountId = getIntent().getStringExtra(ARG_ACCOUNT_ID);
// ** accountId is null **
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.details_container, DetailsFragment.newInstance(accountId))
.commit();
}
}
Intent detailsActivityIntent = new Intent(context, DetailsActivity.class);
intent.putExtra(DetailsActivity.ARG_ACCOUNT_ID, accountId);
detailsActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(detailsActivityIntent);
I do not know if you have messed while posting the code or this is it's actual state but you are setting Extra on intent instance but you are starting activity from detailsActivityIntent instance.

Killing activity in android

Here is the code for back button. I want to kill other activities by back button but its not working in one activity, but I have other activities and without one activity its working fine. Please help me out.
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
SomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Might be this code will help you:
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), SomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
You have to set Flags according to API level :
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
if(Build.VERSION.SDK_INT >= 11)
{
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK);
}
else
{
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
startActivity(intent);
Hope it helps ツ
You can set android:noHistory="true" in activities tag in AndroidManifest.xml which you don't want to save in stack.
Try to define one local broadcast receiver on top most parent activity or base activity :
private final class KillReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
Intialize and register broadcast receiver in onCreate() and unregister in onDestroy() on top most parent activity or base activity :
private KillReceiver clearActivityStack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
clearActivityStack = new KillReceiver();
registerReceiver(clearActivityStack, IntentFilter.create("clearStackActivity", "text/plain"));
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(clearActivityStack);
}
Now call broadcast receiver when wan to clear all previous activity :
public void onClick(View v) {
Intent clearIntent = new Intent("clearStackActivity");
clearIntent.setType("text/plain");
sendBroadcast(clearIntent);
Intent intent = new Intent(getApplicationContext(),SomeActivity.class);
startActivity(intent);
}

Start IntentService from fragment

I'm trying to start an IntentService from a fragment tab but i have no responce. the code from my fragment is below:
private Intent prepareIntent(boolean isSending) {
Intent localIntent = new Intent(getActivity(), StartIActivity.class);
Log.d(THIS_FILE, "StartIActivity");
localIntent.putExtra("incoming", isSending);
localIntent.putExtra("remote_contact", setValidNumber(callUri));
localIntent.putExtra("acc_id", this.accId);
return localIntent;
}
private void startIAService(boolean bool) {
Log.d(THIS_FILE, "Start Service");
Context ctx = (Context) myFragment.this.getActivity();
ctx.startService(prepareIntent(bool));
return;
}
and my intent serviceclass is :
public class StartIActivity extends IntentService {
public StartIActivity() {
super("StartIActivity");
}
protected void onHandleIntent(Intent it) {
Intent intent = new Intent(it);
intent.setClass(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Log.d("IActivity", "Start Activity");
}
}
When run startIAservice the prepereIntent is run but it can't start the service. I need to use IntentService because i want to execute one task at a time but i can't understand how.
Any help here and what is the best code implemenattion to do this?
Declare service in manifest.xml

Categories

Resources