Reputation: 2302
I have two activities,MainActivity
and QuestionActivity
.
MainActivity
is the parent to QuestionActivity
. The problem is when I press the back button/icon of the QuestionActivity
the app exits instead of going back to MainActivty
I ried the following, but yet the app stil doesn't work.
Manifest File
<activity
android:name=".question.QuestionActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
I changed the above to this yet the problem persists
<activity
android:name="full_package_name.question.QuestionActivity"
android:parentActivityName="full_package_name.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="full_package_name.MainActivity" />
</activity>
QuestionActivty
public class QuestionsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
Toolbar toolbar = (Toolbar) findViewById(R.id.questions_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
QuestionsStatePagerAdapter questionsStatePagerAdapter = new QuestionsStatePagerAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.question_container);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setAdapter(questionsStatePagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.question_tabs);
tabLayout.setupWithViewPager(mViewPager);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@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_questions, menu);
return true;
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
//Toast.makeText(this, "Up Button is clicked!", Toast.LENGTH_SHORT).show();
return true;
}
return true;
}
}
EDIT Add MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
public SessionManager sessionManager;
public SQLiteHandler dbHandler;
private DrawerLayout mDrawerLayout;
private NavigationView navigationView;
private View navHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//create a recycleView for the user landing page
TextView userTextView = (TextView) findViewById(R.id.txt_username);
//create dbHandler instance
dbHandler = new SQLiteHandler(getApplicationContext());
//get current user details
HashMap<String, String> currentUser = dbHandler.getUserDetails();
String currentUserName = currentUser.get("username");
String auth_key = currentUser.get("auth_key");
userTextView.setText(auth_key+"\n" + currentUserName);
//in case there is no value for auth_key
//get this guy back to the login Page hehe
//just for enough security
if (auth_key.isEmpty()) {
// Launch Login activity
Intent intent = new Intent(MainActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
}
Toast.makeText(this,"HEY",Toast.LENGTH_LONG).show();
//drawer initialization
mDrawerLayout = (DrawerLayout) findViewById(R.id.app_drawer_layout);
navigationView = (NavigationView) findViewById(R.id.app_navigation);
//setup the header
navHeader = navigationView.getHeaderView(0);
//nav texts
TextView headerUser = (TextView) navHeader.findViewById(R.id.header_user);
headerUser.setText(currentUserName);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.app_navigation);
navigationView.setNavigationItemSelectedListener(this);
//create sessionManger instance
//THE CONTEXT IS SIMPLY THE mAIN ACTIVITY
sessionManager = new SessionManager(getApplicationContext());
sessionManager.isLoggedIn();
//set up the drawer layout
//fetch all the current questions/recent questions
//create a snackbar for asking a question
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.submit_question);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//go to ask question page
//activity or Fragment?
//lets go for fragments
// Launch Login activity
Intent intent = new Intent(MainActivity.this,
QuestionFormActivity.class);
startActivity(intent);
finish();
}
});
}
//override methods
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent intent;
switch (id){
case R.id.nav_blog:
// Launch sales activity
intent = new Intent(
MainActivity.this,
BlogActivity.class);
startActivity(intent);
finish();
break;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.app_drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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.
//The R.id.action_logout is found in the res/menu folder
int id = item.getItemId();
switch (id){
case R.id.action_logout:
logoutCurrentUser();
break;
//implement more actions here
//pull to refresh
}
return super.onOptionsItemSelected(item);
}
}
Is there something I am missing out?
Upvotes: 1
Views: 2441
Reputation: 9225
First initialize the toolbar bar for back button
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
then call back button method this code is enough to go to previous activity else you can use intent method inside
onBackPressed
method to go back
//method on back button click
@Override
public void onBackPressed() {
super.onBackPressed();
/*OR
Intent forgot_password = new Intent( AddItemActivity.this, NavigationAndHomeActivity.class);
startActivity(forgot_password);
finish();
}*/
back button click functionality
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Upvotes: 1
Reputation: 1403
Change your onBackPressed
method like bellow
@Override
public void onBackPressed(){
//remove super.onBackPressed() and you can handle intent to mainActivity or
//any other activity
}
you can also go to manifest.xml
and set parent of your activity to mainActivity after you removed super.onBackPressed()
Upvotes: 3
Reputation: 47
You already have method in your QuestionsActivity i.e onBackPressed
Place your code whatever you want to do in
@Override
public void onBackPressed() {
super.onBackPressed(); //do what ever you want here like going back to your desired screen.
}
Upvotes: 1
Reputation: 1335
Remove meta data from second activity
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="full_package_name.MainActivity" />
Remove it
<activity
android:name=".question.QuestionActivity"></activity>
is enough
and make sure while you write startActivity(intent)
you do not add finish()
after it
Upvotes: 2
Reputation: 1705
that is enough for showing back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
in main activity use intent and startActivity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
in secondary override next function
@Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
(or)
@Override
public boolean onNavigateUp() {
finish();
return true;
}
you can use finish();
to end work with secondary activity and returning to mainActivity
Upvotes: 1