FairEmail/app/src/main/java/eu/faircode/email/FragmentEx.java

130 lines
4.0 KiB
Java
Raw Normal View History

package eu.faircode.email;
2018-08-14 05:53:24 +00:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
2018-08-11 16:13:22 +00:00
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
2018-08-12 17:51:57 +00:00
import android.view.LayoutInflater;
2018-08-06 08:39:28 +00:00
import android.view.View;
2018-08-12 17:51:57 +00:00
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
2018-08-08 06:55:47 +00:00
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
2018-08-22 05:18:31 +00:00
import androidx.lifecycle.Lifecycle;
2018-08-08 06:55:47 +00:00
public class FragmentEx extends Fragment {
2018-08-06 12:05:43 +00:00
private String subtitle = " ";
2018-08-22 05:18:31 +00:00
private boolean finish = false;
protected void setSubtitle(int resid) {
setSubtitle(getString(resid));
}
protected void setSubtitle(String subtitle) {
this.subtitle = subtitle;
updateSubtitle();
}
2018-08-22 05:18:31 +00:00
protected void finish() {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
getFragmentManager().popBackStack();
else
finish = true;
}
2018-08-24 06:43:05 +00:00
@Override
public void onSaveInstanceState(Bundle outState) {
2018-08-27 17:28:49 +00:00
Log.i(Helper.TAG, "Save instance " + this);
2018-08-24 06:43:05 +00:00
super.onSaveInstanceState(outState);
outState.putString("subtitle", subtitle);
}
2018-08-11 16:13:22 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
2018-08-27 17:28:49 +00:00
Log.i(Helper.TAG, "Create " + this + " saved=" + (savedInstanceState != null));
2018-08-11 16:13:22 +00:00
super.onCreate(savedInstanceState);
2018-08-24 06:43:05 +00:00
if (savedInstanceState != null)
subtitle = savedInstanceState.getString("subtitle");
2018-08-11 16:13:22 +00:00
}
2018-08-12 17:51:57 +00:00
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2018-08-16 11:19:19 +00:00
Log.i(Helper.TAG, "Create view " + this);
2018-08-12 17:51:57 +00:00
return super.onCreateView(inflater, container, savedInstanceState);
}
2018-08-11 16:13:22 +00:00
@Override
public void onActivityCreated(Bundle savedInstanceState) {
2018-08-27 17:28:49 +00:00
Log.i(Helper.TAG, "Activity " + this + " saved=" + (savedInstanceState != null));
2018-08-11 16:13:22 +00:00
super.onActivityCreated(savedInstanceState);
}
@Override
public void onResume() {
2018-08-16 11:19:19 +00:00
Log.i(Helper.TAG, "Resume " + this);
super.onResume();
updateSubtitle();
2018-08-22 05:18:31 +00:00
if (finish) {
getFragmentManager().popBackStack();
finish = false;
}
}
2018-08-11 16:13:22 +00:00
@Override
public void onPause() {
2018-08-16 11:19:19 +00:00
Log.i(Helper.TAG, "Pause " + this);
2018-08-11 16:13:22 +00:00
super.onPause();
}
@Override
public void onDetach() {
super.onDetach();
InputMethodManager im = getContext().getSystemService(InputMethodManager.class);
2018-08-06 12:05:43 +00:00
View focused = getActivity().getCurrentFocus();
if (focused != null)
im.hideSoftInputFromWindow(focused.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
2018-08-11 16:13:22 +00:00
@Override
public void onConfigurationChanged(Configuration newConfig) {
2018-08-16 11:19:19 +00:00
Log.i(Helper.TAG, "Config " + this);
2018-08-11 16:13:22 +00:00
super.onConfigurationChanged(newConfig);
}
@Override
public void onDestroy() {
2018-08-16 11:19:19 +00:00
Log.i(Helper.TAG, "Destroy " + this);
2018-08-11 16:13:22 +00:00
super.onDestroy();
}
private void updateSubtitle() {
AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
ActionBar actionbar = activity.getSupportActionBar();
if (actionbar != null)
actionbar.setSubtitle(subtitle);
}
}
}