Tuesday, 18 June 2019

Fragment Navigation Component Graph in Android.


Steps:-
Dependencies:-
implementation 'android.arch.navigation:navigation-fragment:1.0.0'
1. Create a new Project.
2. Create a directory name "navigation" inside res folder like below

3. Right click on navigation directory and add  Navigation Resource File like below :-




4.Click on MainActivity's XML file and do following :-
activity_main:-
here we have to take one fragment which will hold the fragment. And this will be also my hostFragment , 3 important thing don't forget to add:-

android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/login_nav_graph"
app:defaultNavHost="true"


5.Click on navigation folder and click on login_nav_graph.xml and go to design and click on + button and click  "create new designation" and create LoginFragment like below :-
6. be continue to add all classes like that and than the graph will like below:-
7. 

Main Import is:-
import static androidx.navigation.Navigation.findNavController;
code that on click of various button how to write code for navigation:-
public class LoginFragment extends Fragment {
    Button btnMobile, btnFacebook;

    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment        return inflater.inflate(R.layout.fragment_login, container, false);
    }

    @Override    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        btnMobile = view.findViewById(R.id.btnMobile);
        btnFacebook = view.findViewById(R.id.btnFacebook);

        btnMobile.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                findNavController(getActivity(), R.id.btnMobile).navigate(R.id.mobileFragment, null);
            }
        });
        btnFacebook.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                findNavController(getActivity(), R.id.btnFacebook).navigate(R.id.facebookFragment, null);
            }
        });
    }
}








Wednesday, 12 June 2019

Interview Question of Android

1. Android architecture explains all the layer 
2. Where share preferences data is Saving in android 
3. How to make our own app launcher 
4.what is bound service.. how it’s working

5. What is intent filter
Ans:- An Intent filter is an expression in app's manifest file that specifies the type of intent that the component would like to receive  would like to receive . Its like directly start an activity with certain kind of intent.

6. What is component/element of intent filter :-
There are 3 element of intent :-
1. action:- This is mandatory part of the Intent object. Here we r giving predefine action to performed by target activity.
2. data:- this specifies the data type expected by the activity.
3.category:- It is optional part, its like a additional information about the action.

7. How to create table in SQLite.
SQLiteDatabase db = db=openOrCreateDatabase("MyDatabase", Context.MODE_PRIVATE,null);db.execSQL("create table if not exists student(ID integer PRIMARY KEY AUTOINCREMENT NOT NULL,NAME varchar(20),PHONE varchar(11))"); Insert:ContentValues cv=new ContentValues();cv.put("ID",Integer.parseInt(edtId.getText().toString().trim()));
9. What is retrofit :-
8.  How to make todo app in android write steps and flow 

Ans:- Retrofit is REST client for java and android. Its make easy to retrive and upload JSON or other structure data via REST base services. In Retrofit we can configure that which converters factory can use to parsing of data. Typically in Retrofit we are using GSON , but we can also add custom converter factory to process XML or other protocols. Retrofit using IkHttp library for HTTPS request.

10. How to call retrofit
Ans:-  To call Retrofit we basically require 3 things :-
A. Model class of request and response which is used as a JSON model.
B. Interfaces that define the possible HTTPS operations.
C. Retrofit.Builder class :- This class will return the instance of the Interface class , it will also allow to define Base url for HTTP request.
Every method of interface represent  one possible API call.It must have HTTP annotation (GET,POST, etc) which specify the request type and relative URL.
Source:- https://www.journaldev.com/13639/retrofit-android-example-tutorial

11. How R.java is creating in android 
Ans:- Android R.java is an auto-generated file by aapt (Android Asset Packaging Tools) that contain resource IDs for all the resource of res/directory. If we create a XML file than the id for the corresponding component is automatically created in this file, this id use to perform any action on the component.
12. Who is creating r. Java in  android studio :-
aapt (Android Asset Packaging Tool)

13. How  apk in android  creating .

14. What is garbage collection :-
Ans:- It will free unreferenced memory. Internally GC maintaining a tree , in that if any node  not refer  any root than GC will collect that memory and become free that node from memory.
There are 3 steps for the G.C:-
A). Mark:- It will mark all the unreferenced memory.
B). Sweep:- It will delete/free all marked memory.
C). Compaction:- It will compact all free memory to make a bigger chunks.
Types of G.C:-
a. Serial GC :-
When mark and sweep will run all task will stop.
It is bad user experience.
Single threaded process.
b.Parallel GC:-
In this also when mark and sweep run no other thread of application will run.
But in this mark will be multithreaded so mark will completed very fast.
c. Hybrid GC:-
It use CMS (Concurrent mark and sweep Algorithm).
It is same like parallel but in this application thread can run during mark and sweep.

15. What to do when ur app is saying out of memory.
Ans:-
Q. What is Memory leaks.
 A memory leak happen when memory is allocated but never freed.It means memory are out of GC reach.
Its like if we did not free our home for 2 Weeks than imagine what will happen it may smell right? Same way if user keep on using app than memory also keeps on increasing ,if memory leak are not plugged than than unused memory never will free by GC. In this case we will get popup ANR which will crashed the application.

Q. How we can check that our app is leaking?
Ans:- there is an awesome library called "LeakyCabary" which we can use to see leking of memory in app along with stack trace.
Q. Common mistakes that leads to memory leaks.
Ans:-
1. Broadcast Receivers:-  we use a broadcast receiver but we didn't unregister the broadcast receiver then it will holds the reference of activity even if we close the activity. To fix this always we have to call unregister receiver inStop() of activity.Good practice is like always try to call or register a receiver in inStart() or onResume() of activity.
2. Static Activity or View Reference :-
If we reference any view like: TextView,Button as static than the activity would not be garbage collected after it destroyed. So Always remember to Never use static variable for view or activity or contexts.
3. Inner class reference.
Ans:- Never make any static variable inside inner class.  The class should be set to static. Use weak reference of any view and activity.
4. Use applicationContext() instead of activity context if possible.
source:- https://android.jlelse.eu/9-ways-to-avoid-memory-leaks-in-android-b6d81648e35e

16. Screen orientation .. when we change screens orientation where to write toast that screen orientation changes 
17 . How many thing we can call on configuration changed methods 

18. What is AAPT (Android Asset Packaging Tool)) :-
AAPT is a build tool that Android Studio and Android Gradle Plugin use to compile and package your app’s resources. AAPT2 parses, indexes, and compiles the resources into a binary format that is optimized for the Android platform.

19. From which layer of android architecture app will communicate hardware .

20. What is monkey test .
Random test done by QA

21. What is single top which use in android activity 

22. In what format data is saving in shared preference.
Key and value

23. Sensor in android.
24. What is service and its type explain.
25. What is difference between process,thread,task and service.
25. What is component of Android.
26. OOPS concept .
27. Explain following:-
Static,Abstract,Interface,final,synchronized,thread.
28. Write multithreading program.

29. What is RetroLambada.

Retrolambda is a library which allows to use Java 8 lambda expressions, method references and try-with-resources statements on Java 7, 6 or 5.
In earlier versions of Android, Java 8 was not supported. Retrolambda provides a way to use "lambda expressions" on Java versions below 8.
Common examples of lambdas in Android are for click listeners

The Gradle Retrolambda Plug-in allows to integrate Retrolambda into a Gradle based build. This allows for example to use these constructs in an Android application, as standard Android development currently does not yet support Java 8.
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setOnLongClickListener(v -> System.out.println("Long Click"));
30. What is ThreeTenAbp?
https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project

How to secure an Android app as developer side | Security for android app | Secure an Android App

Q. How to secure an Android Application.
Ans:-  1. Use Internal storage for sensitive data.
Every Android App has an internal storage directory associated with it whose path based on app package name. File inside this directory is secure because it use MODE_PRIVATE file creation by default. It mean file cannot be accessed  by any other outside app.
2. Encrypt data on external storage :- The internal storage of an android device are very less. So therefor If at any time if we have to store data on external storage media such as removal SD card tha store data into encrypted  format that data can not access by  any other app.
NOTE:- To write data in encrypt and decrypt we can use javax.crypto package which available in Android SDK.
3. Use Intent for IPC:- To send data to a specific component of an app , we should use Intent rather than use any sockets or shared file. For this we have to create object of an Intent than use setComponent() method to specify package name and to send data use putExtra().
4.Use HTTPS :- All communication between your app and server must be over an HTTPS connection.  Because HTTP request is very easy to hack when we connect with a open wifi in public area.
5.  Use GCM (google cloud data) Instead of SMS.-  SMS protocol is neither encrypted nor safe against spoofing attack, SMS can read by any app which app have READ_SMS permission.
6. Avoid Asking for Personal Data :-  Make login using social login and mobile number login.

Source:- https://code.tutsplus.com/articles/how-to-secure-an-android-app--cms-26385