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);
Related
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()
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.
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);
Actually i am trying to implement you tube API in my android application .so here i am trying to start a activity that's extends a class which defined in same package from my Main Activity using intent but i am getting force to closed....
am giving my code bellow.....i hope anyone can help me and reply me immediately......thank you in advance
my main activity is :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v)
{
Intent in=new Intent();
in.setComponent(new ComponentName(getPackageName(), youtubevideos.class));
startActivity(in);
}
you tube activity is:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v)
{
Intent in=new Intent();
in.setComponent(new ComponentName(getPackageName(), youtubevideos.class));
startActivity(in);
}
`
try
Intent in=new Intent(this, youtubevideos.class);
startActivity(in);
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();
}
});