I have a fragment with WebView inside to open url and login to some service and then receive some cookie. The issue is that when I open application and fragment inside activity, url loads into WebView normally, but when I close application (programmatically with activity.finishAndRemoveTask()) and open it again (manually) - url does not load. After next app restart (manually) everything goes back to normal. Url which I want to open has two redirects.
It is my fragment class:
public class MyFragment extends Fragment implements WebResourceClient.CookieListener {
private String URL = "https://someurl.com";
#OnClick(R.id.close)
public void detachFragment() {
FragmentManager manager = getFragmentManager();
if (manager != null) {
manager.popBackStackImmediate();
}
}
#BindView(R.id.web_container)
WebView webContainer;
#BindView(R.id.progress_bar)
ProgressBar progressBar;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.ny_fragment, container, false);
ButterKnife.bind(this, view);
startWebView();
return view;
}
#SuppressLint("SetJavaScriptEnabled")
private void startWebView() {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(null);
WebSettings settings = webContainer.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
settings.setMediaPlaybackRequiresUserGesture(false);
settings.setUserAgentString(Core.USER_AGENT_FOR_GOOGLE);
settings.setAppCacheEnabled(false);
webContainer.clearCache(true);
WebView.setWebContentsDebuggingEnabled(false);
webContainer.setWebViewClient(new WebResourceClient(this));
webContainer.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress < 100) {
webContainer.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
} else {
webContainer.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
}
}
});
webContainer.loadUrl(URL);
}
#Override
public void onDestroyView() {
webContainer.stopLoading();
super.onDestroyView();
}
#Override
public void onReceiveCookie(String cookie) {
detachFragment();
...
}
#Override
public void showProgress() {
webContainer.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
}
}
It's my WebViewClient:
public class WebResourceClient extends WebViewClient {
private final static String someUrl = "https://someOtherUrl";
private CookieListener listener;
public WebResourceClient(CookieListener listener) {
this.listener = listener;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.contains(someUrl)) {
listener.showProgress();
}
view.loadUrl(url);
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(someUrl)) {
listener.lockWebView();
}
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// if (url.contains(someUrl)) {
String cookies = CookieManager.getInstance().getCookie(url);
String cookie = getCookie(cookies);
if (!TextUtils.isEmpty(cookie)) {
view.stopLoading();
listener.onReceiveCookie(cookie);
}
super.onPageFinished(view, url);
}
private String getCookie(String cookies) {
...
}
public interface GoogleLoginListener {
void onReceiveCookie(String cookie);
void showProgress();
}
}
I open fragment like there:
MyFragment fragment = new MyFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.addToBackStack(fragment.getClass().getSimpleName());
tr.replace(R.id.fragment_root, fragment);
tr.commit();
Related
I have two fragments and one activity (Main Activity). Now when I go from fragment 1 to fragment 2 data in fragment 2 loads from api. But when I press back button, navigates to fragment 1 and again I go from fragment 1 to fragment 2 then the previous loaded data is there for some secs and then fragment loads new data. Why it is so? I want that whenever I go to the fragment 2 it should reload again all the data (without showing the previous data for some secs first). Which method I should use?
Here is my onclick method from fragment 1 for navigating to fragment 2.
private void bind(FragmentUserInputFormBinding binding) {
binding.btnFetchOffer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isAllFieldChecked = validateAllFields(binding);
if (isAllFieldChecked) {
Bundle bundle = new Bundle();
bundle.putString("appid", binding.etAppId.getText().toString());
bundle.putString("uid", binding.etUserId.getText().toString());
bundle.putString("token", binding.etToken.getText().toString());
OfferWallListFragment offerWallListFragment = new OfferWallListFragment();
offerWallListFragment.setArguments(bundle);
requireActivity().getSupportFragmentManager().beginTransaction()
.replace(((ViewGroup) requireView().getParent()).getId(), offerWallListFragment,"offerListFragment")
.setReorderingAllowed(true)
.addToBackStack("offerListFragment")
.commit();
}
}
});
}
Now I have my second fragment designed as follows fragment 2.
#AndroidEntryPoint
public class OfferWallListFragment extends BaseFragment<FragmentOfferWallListBinding> {
private RecyclerViewAdapter recyclerViewAdapter;
private int currentPage = FIRST_PAGE;
public OfferWallListFragment() {
// Required empty public constructor
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Objects.requireNonNull(((AppCompatActivity) requireActivity()).getSupportActionBar()).setTitle("Offers List");
initViewModel(FragmentOfferWallListBinding.bind(view));
}
#NonNull
#Override
protected FragmentOfferWallListBinding initBinding(#NonNull LayoutInflater inflater, #Nullable ViewGroup container) {
return FragmentOfferWallListBinding.inflate(inflater, container, false);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_offer_wall_list, container, false);
}
private void initViewModel(FragmentOfferWallListBinding binding) {
OfferWallViewModel offerWallViewModel = new ViewModelProvider(this).get(OfferWallViewModel.class);
offerWallViewModel.getLiveData(currentPage).observe(getViewLifecycleOwner(), new Observer<OfferResponse>() {
#Override
public void onChanged(OfferResponse offerResponse) {
if (offerResponse != null) {
recyclerViewAdapter = new RecyclerViewAdapter();
recyclerViewAdapter.setOfferResponse(offerResponse);
binding.rvOfferData.setHasFixedSize(true);
binding.rvOfferData.setLayoutManager(new LinearLayoutManager(getContext()));
binding.rvOfferData.setAdapter(recyclerViewAdapter);
binding.rvOfferData.setVisibility(View.VISIBLE);
}
RecyclerViewPaginator recyclerViewPaginator = new RecyclerViewPaginator(binding.rvOfferData) {
#Override
public boolean isLastPage() {
return currentPage == offerResponse.getPages();
}
#Override
public void loadMore(int _currentPage) {
currentPage = _currentPage;
offerWallViewModel.getLiveData(Math.toIntExact(_currentPage));
}
};
binding.rvOfferData.addOnScrollListener(recyclerViewPaginator);
}
});
offerWallViewModel.getNetworkStateMutableLiveData().observe(getViewLifecycleOwner(), new Observer<NetworkState>() {
#Override
public void onChanged(NetworkState networkState) {
if (networkState == NetworkState.Companion.getLOADING()) {
binding.pbLoadingItems.setVisibility(View.VISIBLE);
} else if (networkState == NetworkState.Companion.getERROR()) {
Toast.makeText(getContext(), "Something went wrong, please check the parameters are filled perfectly", Toast.LENGTH_SHORT).show();
if (!CheckInternetConnection.isInternetAvailable(getContext())) {
Toast.makeText(getContext(), "No Internet Connection Available", Toast.LENGTH_SHORT).show();
binding.txtNoConnection.setText("No Internet Connection Available");
binding.txtNoConnection.setVisibility(View.VISIBLE);
}
binding.pbLoadingItems.setVisibility(View.GONE);
} else {
binding.pbLoadingItems.setVisibility(View.GONE);
}
}
});
}
}
Also I have handled onBackPressed method from MainActivity as follows.
#AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
if (navHostFragment != null) {
NavController navController = navHostFragment.getNavController();
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.userInputFormFragment, R.id.offerWallListFragment
).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
}
}
#Override
public void onBackPressed(){
int count = getSupportFragmentManager().getBackStackEntryCount();
if(count == 0) {
super.onBackPressed();
} else {
getSupportFragmentManager().popBackStack();
}
}
}
Please have a look on it ?
I am trying to load a url with webview that loads perfectly fine on mobile using chrome, but the website says "Website temporarily unavailable". I tried changing the user agent to no avail. I can't figure out for the life of me why this is happening. The url in question is https://www.manototv.com/news
A screenshot of loading with webview
A screenshot of loading with chrome
Relevant portion of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
mWebView = findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
mWebView.setWebViewClient(new MyWeb());
mWebView.setWebChromeClient(new MyChrome());
webSettings.setJavaScriptEnabled(true);
webSettings.setMediaPlaybackRequiresUserGesture(false);
mWebView.loadUrl(path)
}
class MyWeb extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());
mWebView.loadUrl(SCRIPT_FULLSCREEN);
mWebView.loadUrl(SCRIPT_CLICK);
mWebView.loadUrl(SCRIPT_PLAY);
mWebView.setVisibility(View.VISIBLE);
}
}
private class MyChrome extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
#Override
public Bitmap getDefaultVideoPoster() {
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
#Override
public void onHideCustomView() {
((FrameLayout) getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
#Override
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
if (this.mCustomView != null) {
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout) getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846);
}
}
I want to integrate my app with Microsoft Office 365 Outlook to be able to manage user's account. The user may access his account and may see email without a problem but when the user wants to download attached files program closes immediately.
I tried almost every suggestion on Stack Overflow which exists in other posts and couldn't manage to find the true solution. I also researched about outlook account management for Android and couldn't find helpful information. So I need some suggestion and solutions to handle this.
public class hiddenNetwork extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private WebView mWebview;
ProgressDialog prDialog;
public hiddenNetwork() {
// Required empty public constructor
}
private Handler handler = new Handler(){
#Override
public void handleMessage(Message message) {
switch (message.what) {
case 1:{
webViewGoBack();
}break;
}
}
};
public static hiddenNetwork newInstance(String param1, String param2) {
hiddenNetwork fragment = new hiddenNetwork();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
int count=0;
private void webViewGoBack(){
count++;
mWebview.goBack();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_announcement, container, false);
mWebview = (WebView)view.findViewById(R.id.webView);
CookieManager cookieManager = CookieManager.getInstance();
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebview.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
DownloadManager dm = (DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
}
});
mWebview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".pdf")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// if want to download pdf manually create AsyncTask here
// and download file
return true;
}
return false;
}
#Override public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
super.onReceivedError(view, request, error);
}
});
mWebview.loadUrl("https://login.microsoftonline.com");
return view;
}
public void myOnKeyDown(int key_code){
//do whatever you want here
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
I have a Fragment that contains a RecyclerView. I am trying to implement a filter on the RecyclerView. The filter UI opens a new Fragment Dialog where the user will input a value. Once the user hits the Search Button in the Fragment Dialog, the value should be returned to the RecyclerView Fragment and the existing data in the view should be cleared. I want to re-populate the RecyclerView with the new set of data that I will obtain from the server. My problem is that, I have a method called resetAdapterDetails() in the RecyclerView Fragment which works as expected if called from the RecyclerView Fragment itself. But, when I try to call the same method from the Fragment Dialog, I get an exception:
transactionList.clear(); --> is trying to clear a list which is null
Though the data is still visible in the RecyclerView.
The RecyclerView Fragment:
public class TransactionHistoryFragment extends Fragment implements SearchView.OnQueryTextListener, DateRangePickerFragment.OnDateRangeSelectedListener{
private RecyclerView recyclerview;
private TransactionHistoryAdapter adapter;
private List<Transaction> transactionList;
public TransactionHistoryFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_transaction_history, container, false);
recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(layoutManager);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
getTransactionHistory("");
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.transactions_history_menu, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.filter_date:
FragmentManager fmDate = getActivity().getFragmentManager();
DateRangePickerFragment dialogFragmentDate = DateRangePickerFragment.newInstance(this, true);
dialogFragmentDate.show(fmDate, "Sample Fragment");
return true;
case R.id.filter_mobile:
FragmentManager fmMobile = getActivity().getFragmentManager();
SearchMobileFragment dialogFragmentMobile = new SearchMobileFragment ();
dialogFragmentMobile.show(fmMobile, "Sample Fragment");
//resetAdapterDetails();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onQueryTextChange(String newText) {
final List<Transaction> filteredModelList = filter(transactionList, newText);
adapter.setFilter(filteredModelList);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
//for filtering the list
private List<Transaction> filter(List<Transaction> models, String query) {
query = query.toLowerCase();final List<Transaction> filteredModelList = new ArrayList<>();
for (Transaction model : models) {
final String text = model.getTxnStatus().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
//for populating the list
private void getTransactionHistory(String agentId){
GetTransactionHistoryTask task = new GetTransactionHistoryTask("agent1", "password");
task.getTransactionsByAgent("OU23","OU230000000123456789").subscribe(transactionHistoryResponse -> {
if(transactionHistoryResponse != null && transactionHistoryResponse.getTransactions() != null && transactionHistoryResponse.getTransactions().size() > 0 && transactionHistoryResponse.getErrors().size() == 0){
transactionList = transactionHistoryResponse.getTransactions();
adapter = new TransactionHistoryAdapter(transactionList);
recyclerview.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
recyclerview.setAdapter(adapter);
onClickListnerRecyclerView();
}
else{
}
}, e -> e.printStackTrace());
}
private void onClickListnerRecyclerView() {
recyclerview.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
try {
final Transaction transactionModel= (Transaction) adapter.getObjectAt(position);
Intent i = new Intent(getActivity(), TransactionDetailsActivity.class);
i.putExtra("transaction_object",transactionModel);
startActivity(i);
}
catch (Exception e){
Log.e("List issue", e.toString());
}
}
})
);
}
#Override
public void onDateRangeSelected(int startDay, int startMonth, int startYear, int endDay, int endMonth, int endYear) {
}
public void fetchDateRange(String startDate, String endDate) {
Log.e("DateRange",startDate + "\n" + endDate);
}
public void fetchMobileNumber(String mobileNumber) {
Log.e("Mobile",mobileNumber);
resetAdapterDetails();
}
public boolean resetAdapterDetails(){
try {
transactionList.clear();
adapter.notifyDataSetChanged();
recyclerview.setAdapter(adapter);
} catch (Exception e) {
Log.e("Reset Error", ""+e.getMessage());
}
return true;
}
}
The Dialog Fragment:
public class SearchMobileFragment extends DialogFragment {
EditText mMobileNumberEditText;
Button search_button;
public SearchMobileFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_search_mobile, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMobileNumberEditText = (EditText) view.findViewById(R.id.mobile_number_editText);
search_button = (Button) view.findViewById(R.id.search_button);
search_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
TransactionHistoryFragment obj = new TransactionHistoryFragment();
obj.fetchMobileNumber(mMobileNumberEditText.getText().toString());
}
});
}
}
The fetchMobileNumber() method in the TransactionHistoryFragment (RecyclerView Fragment) is called through the fetchMobileNumber() method which is called from the SearchMobileFragment (Dialog Fragment).
Where am I going wrong? Why the transactionList.clear(); is throwing the null pointer exception?
You are getting the issue because you are creating new TransactionHistoryFragment instance search_button click in SearchMobileFragment. Which makes it's all variables null and initialize it again and here your transactionList becomes null.
You can achieve the same thing easily with different way also. As the place of SearchMobileFragment as a DialogFragment you can make it as Activity and start it as startActivityForResult from your TransactionHistoryFragment and implement onActivityResult callback to doing the fiteration.
But right now in your case you can manage it in different ways also:
First way:
As you are doing in your DialogFragment
search_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
/*TransactionHistoryFragment obj = new TransactionHistoryFragment();
obj.fetchMobileNumber(mMobileNumberEditText.getText().toString());*/
}
});
Don't do the call for fetchMobileNumber here, in onResume of your TransactionHistoryFragment you should make a call for this. On the click of search_button save the filter data to SharedPreferences and use that in onResume of the TransactionHistoryFragment for filtering and after that clear this saved data from SharedPreferences onPause of this fragment.
You should remove
transactionList.clear();
of code from resetAdapterDetails() in TransactionHistoryFragment, because after search filter you will get updated transactionList which is already passed to adapter then forcefully no need to clear it. Or have a check before clearing it like:
if(transactionList!=null){
transactionList.clear();
}
Second way: Using BroadcastReceiver you can achieve the same thing.
Register a receiver in your TransactionHistoryFragment and sendBroadcast from SearchMobileFragment. In onReceive of the TransactionHistoryFragment do the filtration process.
I had resolved the above issue in a different way. In the Dialog Fragment I have implemented a View.OnClickListener and have created an Interface to initialize the same from the RecyclerView Fragment. I am posting the complete source codes below; the SearchMobileFragment now looks like:
public class SearchMobileFragment extends DialogFragment implements View.OnClickListener{
private OnMobileNumberSelectedListener onMobileNumberSelectedListener;
EditText mMobileNumberEditText;
Button mSearchButton;
public SearchMobileFragment() {
// Required empty public constructor
}
public static SearchMobileFragment newInstance(OnMobileNumberSelectedListener callback) {
SearchMobileFragment searchMobileFragment = new SearchMobileFragment();
searchMobileFragment.initialize(callback);
return searchMobileFragment;
}
public void initialize(OnMobileNumberSelectedListener callback) {
onMobileNumberSelectedListener = callback;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_search_mobile, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mMobileNumberEditText = (EditText) root.findViewById(R.id.mobile_number_editText);
mSearchButton = (Button) root.findViewById(R.id.search_button);
mSearchButton.setOnClickListener(this);
return root;
}
#Override
public void onStart() {
super.onStart();
if (getDialog() == null)
return;
getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}
public void setOnMobileNumberSelectedListener(OnMobileNumberSelectedListener callback) {
this.onMobileNumberSelectedListener = callback;
}
#Override
public void onClick(View v) {
dismiss();
onMobileNumberSelectedListener.onMobileNumberSelected(mMobileNumberEditText.getText().toString());
}
public interface OnMobileNumberSelectedListener {
void onMobileNumberSelected(String mobileNumber);
}
}
The RecyclerView Fragment modifications:
public class TransactionHistoryFragment extends Fragment implements SearchView.OnQueryTextListener,
DateRangePickerFragment.OnDateRangeSelectedListener, SearchMobileFragment.OnMobileNumberSelectedListener{
private RecyclerView recyclerview;
private TransactionHistoryAdapter adapter;
private List<Transaction> transactionList;
SearchView search;
public static final String TIMERANGEPICKER_TAG = "timerangepicker";
public TransactionHistoryFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_transaction_history, container, false);
recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(layoutManager);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
getTransactionHistory();
}
#Override
public void onResume() {
Log.e("onResumeTHF","invoked");
super.onResume();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.transactions_history_menu, menu);
search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.filter_date:
FragmentManager fmDate = getActivity().getFragmentManager();
DateRangePickerFragment dialogFragmentDate = DateRangePickerFragment.newInstance(this, true);
dialogFragmentDate.show(fmDate, "Sample Fragment");
return true;
case R.id.filter_mobile:
FragmentManager fmMobile = getActivity().getFragmentManager();
SearchMobileFragment dialogFragmentMobile = SearchMobileFragment.newInstance(this);
dialogFragmentMobile.show(fmMobile, "Sample Fragment");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onQueryTextChange(String newText) {
Log.e("newText",newText);
final List<Transaction> filteredModelList = filter(transactionList, newText);
adapter.setFilter(filteredModelList);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public void onMobileNumberSelected(String mobileNumber) {
Log.e("mobileNumber",mobileNumber);
resetAdapterDetails();
}
public boolean resetAdapterDetails(){
try {
transactionList.clear();
adapter.notifyDataSetChanged();
recyclerview.setAdapter(adapter);
} catch (Exception e) {
Log.e("Reset Error", ""+e.getMessage());
}
return true;
}
}
Happy coding!
The null pointer exception is because when you create a new TransactionHistoryFragment using new onViewCreated is not called and hence transactionList is never initialized. You can create a setter for the list or pass it as a constructor to the fragment to fix this
I'm trying to use a WebView to display a webpage in my application. At first, I didn't have any permissions in the AndroidManifest.xml and I got the error 'the webpage at could not be loaded because net::ERR_CACHE_MISS'
But now that I have added the permission to the AndroidManifest.xml, the WebView loads as a empty white box.
Here is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package= ... >
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</application>
</manifest>
And my Java class:
public class location_Fragment extends Fragment {
private WebView webView;
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.location_layout, container, false);
webView = (WebView)rootview.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
return rootview;
}
}
Try this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.location_layout, container, false);
webView = (WebView)rootview.findViewById(R.id.webView);
webView.setWebViewClient(new MyBrowser());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
return rootview;
}
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Use The Following Code
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Bundle b = getIntent().getExtras();
String data =b.getString("link");
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
if(data.equals("")||data.equals("null")) {
Toast.makeText(WebViewActivity.this,"No Link Available",Toast.LENGTH_SHORT).show();
}
{
webView.loadUrl(data);
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
// activity.setProgress(progress * 1000);
WebViewActivity.this.setTitle("Loading...");
WebViewActivity.this.setProgress(progress * 100);
if(progress == 100)
WebViewActivity.this.setTitle(R.string.app_name);
}
});
}
private class MyWebViewClient extends WebViewClient {
private ProgressDialog pDialog;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// pDialog = new ProgressDialog(WebViewActivity.this,R.style.DialogTheme);
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
if (pDialog == null) {
// in standard case YourActivity.this
pDialog = new ProgressDialog(WebViewActivity.this,R.style.DialogTheme);
pDialog.setMessage("Loading...");
pDialog.show();
}
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
public void onPageFinished(WebView view, String url) {
try{
pDialog.dismiss();
if (pDialog.isShowing()) {
pDialog.dismiss();
pDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}