AndroidRestart Activity from PreferenceFragment - android

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.

Related

Android Studio Activity wont start if dont finish()

I try to run LogIn activity from MainActivity but if I dont add FINISH() after
startActivityForResult, intent wont start
on MainActivity
**pref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean mojpin = pref.getBoolean("PinLock",false);
if(mojpin && loged==false){
Intent i = new Intent(MainActivity.this,AppPinLock.class);
MainActivity.this.startActivityForResult(i,1);
finish();
}**
On LogIn Activity
**String mojpin = getPassword();
if(mojpin!= ""){
String mojpin2 = pass.getText().toString();
if(mojpin.equals(mojpin2)){
naslov.setText("Pin is ok");
Intent i = new Intent(AppPinLock.this,MainActivity.class);
setResult(1, i);
startActivity(i); //if I dont add this wont back to MainActivity
finish();**
Yeah, You shouldn't close MainActivity after calling startActivity();
Explanation: If you start an Activity in MainActivity, the Method
#Override
protected void onPause() {
super.onPause();
}
Will be called. But if you call finish(); after calling the other Activity in the MainActivity, Android instantly Terminates the MainActivity completly and you can't get back until you call the MainActivity again. Example Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this, Activity2.class));
//DO NOT use finish(); here!!! (It will kill this Activity after launching Actiivty2)
}
}
Activity2
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
finish(); //this will close this Activity and you will get back to the last Activity. (In this case MainActivity)
}
}

How to kill background Activity in Android studio

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()

How to call all activity onDestroy() when MainAcitivty call onDestroy()?

I made two activities for the test.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SubActivity.class));
}
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
This button in MainActivity can start SubActivity.
public class SubActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
public void onBackPressed() {
startActivity(new Intent(SubActivity.this, MainActivity.class));
}
}
When in SubActivity, press back button, it doesn't call onDestroy() in SubActivity class and start MainActivity. What I want to do is, How to call SubActivity onDestroy() when I finish the MainActivity? It doesn't call SubActivity onDestroy() when I press the back button in MainActivity. Is there any solution like using Intent.FLAG_ACTIVITY?
You should finish() your SubActivity in onBackPressed to destroy it and go back to your MainActivity, instead of doing a startActivity. In many cases, that is the behavior you'll get even if you don't implement onBackPressed yourself. Your current code is starting a second MainActivity.
The solution proposed here worked for me:
In Java:-
Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
In Kotlin:-
val i = Intent(OldActivity.this, NewActivity::class.java)
// set the new task and clear flags
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(i)

getting force close when i pass intent botween two activities using setcomponentname cunstructor

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);

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);

Categories

Resources