Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 在 choose area 界面按返回按钮时,逐级返回,而不是直接退出 app #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -272,4 +271,24 @@ private void closeProgressDialog() {
}
}

/**
* 托管此 fragment 的 activity 可以调用此方法, 以便给此 fragment 一个处理点击返回按钮的机会
* @return true 表示此 fragment 处理了点击返回按钮的事件, activity 可以不处理了, false 表示 fragment 不处理
*/
public boolean onBackPressed() {
if (currentLevel == LEVEL_COUNTY) {
currentLevel = LEVEL_CITY;
queryCities();
} else if (currentLevel == LEVEL_CITY) {
currentLevel = LEVEL_PROVINCE;
queryProvinces();
} else {
return false;
}
return true;
}

public int getCurrentLevel() {
return currentLevel;
}
}
18 changes: 17 additions & 1 deletion app/src/main/java/com/coolweather/android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
ChooseAreaFragment mFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 通过代码来托管 fragment
FragmentManager fm = getSupportFragmentManager();
mFragment = (ChooseAreaFragment) fm.findFragmentById(R.id.fragment_container);
if (mFragment == null) {
mFragment = new ChooseAreaFragment();
fm.beginTransaction().add(R.id.fragment_container, mFragment).commit();
}

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getString("weather", null) != null) {
Intent intent = new Intent(this, WeatherActivity.class);
Expand All @@ -21,4 +31,10 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

@Override
public void onBackPressed() {
if (!mFragment.onBackPressed()) {
super.onBackPressed();
}
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/coolweather/android/WeatherActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
Expand Down Expand Up @@ -66,6 +67,8 @@ public class WeatherActivity extends AppCompatActivity {

private String mWeatherId;

private ChooseAreaFragment chooseAreaFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -106,6 +109,15 @@ protected void onCreate(Bundle savedInstanceState) {
weatherLayout.setVisibility(View.INVISIBLE);
requestWeather(mWeatherId);
}

// 通过代码来托管 fragment
FragmentManager fm = getSupportFragmentManager();
chooseAreaFragment = (ChooseAreaFragment) fm.findFragmentById(R.id.fragment_container);
if (chooseAreaFragment == null) {
chooseAreaFragment = new ChooseAreaFragment();
fm.beginTransaction().add(R.id.fragment_container, chooseAreaFragment).commit();
}

swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Expand Down Expand Up @@ -235,4 +247,19 @@ private void showWeatherInfo(Weather weather) {
startService(intent);
}

@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
if (chooseAreaFragment.getCurrentLevel() == ChooseAreaFragment.LEVEL_PROVINCE) {
// 在 choose area 时如果是省一级的, 点击返回时关闭 drawer 视图
drawerLayout.closeDrawers();
} else {
// 如果是市或者区一级的, 就让 fragment 处理点击返回按钮事件, 即返回更大一级的 choose area 界面
chooseAreaFragment.onBackPressed();
}
} else {
// drawer 未显示, 则直接退出 app
super.onBackPressed();
}
}
}
8 changes: 1 addition & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/choose_area_fragment"
android:name="com.coolweather.android.ChooseAreaFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>
12 changes: 6 additions & 6 deletions app/src/main/res/layout/activity_weather.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@

</android.support.v4.widget.SwipeRefreshLayout>

<fragment
android:id="@+id/choose_area_fragment"
android:name="com.coolweather.android.ChooseAreaFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_gravity="start"
/>
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>

</android.support.v4.widget.DrawerLayout>

Expand Down