How to kill background Activity in Android studio - android

I have the MainActivity and the secon_Activity in Android Studio, from MainActivity I go to second_Activity using Intent, then in the second_Activity I want to finish that activity, I do it but the first activity remains in background, how can I do to end or kill both activities.
Here the MainActivity:
public class MainActivity extends AppCompatActivity {
Button send_button;
EditText send_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_activity);
send_button = (Button)findViewById(R.id.send_button_id);
send_text = (EditText)findViewById(R.id.send_text_id);
send_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
String str = send_text.getText().toString();
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
intent.putExtra("message_key", str);
startActivity(intent);
finish();
}
});
}
}
Here de second_Activity:
public class Second_activity extends AppCompatActivity {
TextView receiver_msg;
Button finish_button;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
receiver_msg = (TextView)findViewById(R.id.received_value_id);
finish_button = (Button)findViewById(R.id.finish_button_id);
Intent intent = getIntent();
String str = intent.getStringExtra("message_key");
receiver_msg.setText(str);
finish_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
finish();
}
});
}
}

I've built a Kiosk app and from my searching you can't remove it from the "recent" apps.
You can how ever, use ActivityManager as follows:
ActivityManager am = (ActivityManager) Context.getSystemService(ACTIVITY_SERVICE);
am.moveTaskToFront(myAppId, 0);
am.killBackgroundProcesses(Activity_A_Package_Name);
Requires:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
It should kill it, usually.

you can use System.exit(1) instead of finish()

Related

AndroidRestart Activity from PreferenceFragment

I declared my PreferenceFragment within a SettingsActivity like this
public class ChordsSettings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new MyPreferenceFragment())
.commit();
}
public static class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preferences);
}
}
}
I did it like this and didn't create a Fragment on top of the MainActivity because I need to be able to use the back button to get from the SettingsActivity to the MainActivity and this seamed like the only way to go to achieve that.
I need to restart my MainActivity once the preferences changed.
I tried sending a Broadcast from the preferenceFragment but sendBroadcast() cannot be used from a static context. Is there any other way I can achieve this?
Your MainActivity should probably look like this. Note that when btnGoToSetting is clicked finish() method is called. This is for closing the currrent Activity.
MainActivity.java
public class MainActivity extends Activity {
Button btnGoToSetting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGoToSetting = (Button)findViewById(R.id.btnGoToSetting);
btnGoToSetting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
});
}
}
SettingsActivity.java
public class SettingsActivity extends Activity {
Button btnSavePreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnSavePreferences = (Button)findViewById(R.id.btnSavePreferences);
btnSavePreferences.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here is where you save all your preferences
yourSaveFunction();
finish();
Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed() {
finish();
Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
startActivity(intent);
}
}
Note : There is another way to resfresh you data in MainActivity by making your data asynchronous so that everytime the data changes it will be refresh automatically.

How to close an activity from another activity Android

I am developing android app in which I have 2 activities. I want to close activity A from activity B on button press and recreate activity A. How to do it need help?
You can use sendBroadcast method, by this way you can close one or more activities.
In your ActivityB use this code:
public class ActivityA extends AppCompatActivity {
public static final String FINISH_ALERT = "finish_alert";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.registerReceiver(this.finishAlert, new IntentFilter(FINISH_ALERT));
}
BroadcastReceiver finishAlert = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
ActivityA.this.finish();
}
};
#Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(finishAlert);
}
}
and in your ActivityB call this command to finish it:
Intent i = new Intent(ActivityA.FINISH_ALERT);
this.sendBroadcast(i);

How To Open The Last Opened Activity After Pause The Application

I try to to add an Splash Activity to my application
this splash activity open when the program start for first time
but when the app work and skip the splash activity to main activity
and user hold on and go to the home screen
after that the user will return to the app ,,
the problem is here
so the application reopen the splash activity again
i don't want the app to open the splash activity every time the application resume activity
i need to direct open the main activity ...
I tried this code but it doesn't work with me
In Splash Activity ...
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Class<?> activityClass;
try
{
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(prefs.getString("lastActivity", MainActivity.class.getName()));
}
catch(ClassNotFoundException ex)
{
activityClass = MainActivity.class;
}
startActivity(new Intent(this, activityClass));
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
}
);
}
}
and in the Main Activity i use this code
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
}
Use Application class to save a global flag:
public class App extends Application {
private static boolean sFirstRun = false;
public static boolean fetchFirstRun() {
boolean old = sFirstRun;
sFirstRun = false;
return old;
}
//--called when app process is created--
#Override
public void onCreate() {
super.onCreate();
sFirstRun = true;
}
}
And in manifest.xml add : <application android:name=".App"
now, App.fetchFirstRun() is true only when App process is created from scratch. As long as app is running subsequent calls will return false.

Activity moved to back starting activity

I find it difficult to start new activity using application staying in the background.
Here is my code:
public class App1 extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context c, Intent i) {
Toast.makeText(getApplicationContext(), "TEST", Toast.LENGTH_LONG).show();
Intent intent = new Intent("App2.intent.action.Launch");
intent.putExtra("startedByApp", true);
startActivity(intent);
}
}
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
}
public class App2 extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getApplicationContext(), "TEST 2", Toast.LENGTH_LONG).show();
}
}
When App1 is in the foreground it works fine.
When App1 is in the background (moveToBack) it shows "TEST", but it doesn't start App2 (there is no "TEST 2" on my screen.
Guys, can you help me?
You need a flag intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK);

Multiple page switching in android eclipse

Consider i am using five screen pages for project "A".Each page is having switching between other pages sequentially one by one,my need is to do close all the page when i am clicking the button "exit" from the page five which is the last one.
I have used this below code,but the problem is only the last page is getting close others are not.
find my code below
Button extbtn = (Button)findViewById(R.id.but_Exit);
extbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
} });
Thanks for your time!
Make all five activities extend a BaseActivity that registers a BroadcastReceiver at onCreate (and unregisters at onDestroy).
When extbtn is clicked, send a broadcast to all those BaseActivities to close themselves
for example, in your BaseActivity add:
public static final String ACTION_KILL_COMMAND = "ACTION_KILL_COMMAND";
public static final String ACTION_KILL_DATATYPE = "content://ACTION_KILL_DATATYPE";
private KillReceiver mKillReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
...
mKillReceiver = new KillReceiver();
registerReceiver(mKillReceiver, IntentFilter.create(ACTION_KILL_COMMAND, ACTION_KILL_DATATYPE));
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mKillReceiver);
}
private final class KillReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
and at extbtn's onClick call:
extbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// send a broadcast that will finish activities at the bottom of the stack
Intent killIntent = new Intent(BaseActivity.ACTION_KILL_COMMAND);
killIntent.setType(BaseActivity.ACTION_KILL_DATATYPE);
sendBroadcast(killIntent);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});

Categories

Resources