Reputation: 153
*UPDATE! About this link - Android close app on back button - I have tried code from answers, but nothing is changing. I think my problem is in Activity A. When user open my app, Activity A check does there is a value in preferences. If value is missing, Activity A doesn't refer user to Activity B. If value is present Activity A refer user to Activity B. So it is a vicious cycle: User open my app ---> He is get to Activity A ---> Activity A checks value in preferences ---> Activity A refer user to Activity B ---> When user clicks back button, Activity A is is opening ---> Activity A refer user to Activity B ---> and so on
Please help me to solve my problem)
I have two activities - A and B. Activity A have a button, when I click it, it refer me to the settings. But if it is a value in the settings, then Activity A refer me to Activity B using Intent. Activity B is activity with content, and when I press back button Activity B restarts. But I want to close the app, when back button is clicked. How I can do that?
My code:
AActivity.java:
public class AActivityextends AppCompatActivity implements View.OnClickListener {
Button btn_select;
SharedPreferences sp;
/**
* Called when the activity is first created.
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
btn_select = (Button) findViewById(R.id.btn_select);
sp = PreferenceManager.getDefaultSharedPreferences(this);
String select_class = sp.getString("select_class", "");
if (select_class.equals("") || select_class.equals("0")) {
btn_select.setVisibility(View.VISIBLE);
}
}
protected void onResume() {
String select_class = sp.getString("select_class", "");
tvInfo.setText(select_class);
if (select_class.equals("") || select_class.equals("0")) {
btn_select.setVisibility(View.VISIBLE);
} else {
btn_select.setVisibility(View.INVISIBLE);
Intent intent = new Intent(this, BActivity.class);
startActivity(intent);
}
super.onResume();
}
}
BActivity.java:
public class BActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timetable);
sp = PreferenceManager.getDefaultSharedPreferences(this);
String select_class = sp.getString("select_class", "");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new MondayFragment(), "ПН");
adapter.addFragment(new TuesdayFragment(), "ВТ");
adapter.addFragment(new WednesdayFragment(), "СР");
adapter.addFragment(new ThursdayFragment(), "ЧТ");
adapter.addFragment(new FridayFragment(), "ПТ");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(BActivity.this, SettingsActivity.class);
intent.putExtra("fwflag","from_menu_item");
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}
My AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.x.x">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity android:name=".AActivity"
android:theme="@style/MyMaterialTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:theme="@style/MyMTheme.Base" />
<activity android:name=".BActivity"
android:theme="@style/MyMTheme">
</activity>
</application>
My Activity C code:
public class CActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
//toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
getFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PrefFragment()).commit();
}
public class PrefFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref);
Intent intent = getIntent();
String flag_from_where = intent.getStringExtra("fwflag");
if (flag_from_where.equals("from_btn_select")) {
PreferenceScreen main = (PreferenceScreen) findPreference(getString(R.string.pref_screen));
main.onItemClick(null, null, 0, 0);
} else if (flag_from_where.equals("from_menu_item")) {
}
final ListPreference list = (ListPreference) getPreferenceScreen().findPreference("listpref");
list.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Intent intent = new Intent(CActivity.this, BActivity.class);
startActivity(intent);
finish();
return true;
}
});
}
}
}
Upvotes: 1
Views: 2214
Reputation: 3572
You need to "finish" each activity right after you call to start a new activity.
So after every startActivity(intent)
you should add finish()
right below it.
That should take care of your problem.
Upvotes: 2