我有一个主要活动(OrganizerMainActivity
),其RecyclerView
。当您单击列表项时,我会以这种方式启动一个新的活动:
@Override
public void onListFragmentInteraction(EventModel item) {
Intent eventDetailIntent = new Intent(this, EventDetailActivity.class);
eventDetailIntent.putExtra(EVENT_ITEM, item);
startActivity(eventDetailIntent);
}
在我来到EventDetailActivity
之后,如果按下后退按钮,整个应用程序都会关闭。但是,我需要该应用返回OrganizerMainActivity
。
我的AndroidManifest.xml
也是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.iamindia.com.iamindia">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
"
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SignInActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SignUpActivity" />
<activity android:name=".UserTypeActivity" />
<activity
android:name=".OrganizerMainActivity"
android:configChanges="orientation"
android:label="@string/title_activity_main"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" />
<activity
android:name=".ForgotPasswordActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".SignUpOneActivity"
android:label="SignUpActivity"
android:theme="@style/AppTheme" />
<activity
android:name=".VolunteerMainActivity"
android:configChanges="orientation"
android:label="@string/title_activity_volunteer_main"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" />
<activity android:name=".SignUpTwoActivity" />
<activity
android:name=".CreateEventActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCh0sjei_PUUCGexYBZtGbDjN_pZGNjCXs" />
<activity
android:name=".EventDetailActivity"
android:label="@string/title_activity_event_detail"
android:theme="@style/AppTheme"
android:parentActivityName=".OrganizerMainActivity"></activity>
</application>
更新:EventDetailActivity
package app.iamindia.com.iamindia;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import app.iamindia.com.iamindia.models.EventModel;
import app.iamindia.com.iamindia.models.UserModel;
import app.iamindia.com.iamindia.services.UserService;
public class EventDetailActivity extends AppCompatActivity {
private static final String EVENT_ITEM = "EVENT_ITEM";
private EventModel item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Event Detail");
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();
}
});
if (getIntent().getExtras().getParcelable(EVENT_ITEM) != null) {
item = getIntent().getExtras().getParcelable(EVENT_ITEM);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
private void getOrganizerProfileDetails(String organizerId) {
UserService.getInstance().getUserProfile(organizerId, new UserService.AsyncListener<UserModel>() {
@Override
public void onSuccess(UserModel userModel) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
更新:OrganizerMainActivity
package app.iamindia.com.iamindia;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.List;
import app.iamindia.com.iamindia.models.EventModel;
import app.iamindia.com.iamindia.services.EventService;
import app.iamindia.com.iamindia.services.UserService;
public class OrganizerMainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, HomeFragment.OnFragmentInteractionListener,
EventListFragment.OnListFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener,
LeaderBoardFragment.OnFragmentInteractionListener{
private static int navIndex = 0;
private static String currentTag = "home";
private String[] activityTitles;
private NavigationView navigationView;
private static final String EVENT_ITEM = "EVENT_ITEM";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_organizer_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
if (UserService.getInstance().getCurrentUser().getType().equalsIgnoreCase("organizer")) {
fab.setVisibility(View.VISIBLE);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent newEventIntent = new Intent(OrganizerMainActivity.this, CreateEventActivity.class);
startActivity(newEventIntent);
}
});
} else {
fab.setVisibility(View.GONE);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
activityTitles = getResources().getStringArray(R.array.nav_item_titles);
// load home fragment by default
if (savedInstanceState == null) {
navIndex = 0;
currentTag = "home";
loadHomeFragment();
}
EventService.getInstance().fetchEvents(new OnSuccessListener<List<EventModel>>() {
@Override
public void onSuccess(List<EventModel> eventModels) {
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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 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) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
displayFragment(item.getItemId());
return true;
}
private void displayFragment(int id) {
Fragment fragment;
switch(id) {
case R.id.organizer_home:
navIndex = 0;
fragment = new HomeFragment();
setTitle("Home");
break;
case R.id.organizer_leaderboard:
navIndex = 1;
fragment = new LeaderBoardFragment();
setTitle("Leader Board");
break;
default:
fragment = new HomeFragment();
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
private void loadHomeFragment() {
selectNavMenu();
setToolbarTitle();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, new HomeFragment());
ft.commit();
}
private void selectNavMenu() {
navigationView.getMenu().getItem(navIndex).setChecked(true);
}
private void setToolbarTitle() {
// getSupportActionBar().setTitle(activityTitles[navIndex]);
}
@Override
public void onFragmentInteraction(Uri uri){
//you can leave it empty
}
@Override
public void onListFragmentInteraction(EventModel item) {
Intent eventDetailIntent = new Intent(this, EventDetailActivity.class);
eventDetailIntent.putExtra(EVENT_ITEM, item);
startActivity(eventDetailIntent);
}
}
有人可以指导我吗?