How to handle
async Task on activity/fragment during rotation?.
I have two options for maintain
state of activity or fragment during rotation.
v Use Intent
Service and Broadcast Receiver and send data on Intent Service when screen
rotate and get data from Broadcast receiver on onRestoreInstantState() method
v
In
Fragment we are use two component first is interface that is work as a callback
methods and second is android system provided method by name setRetainState(true) on Oncreate() method.
Difference
between Serializable and Parcelable.
Serializable: Serializable is used
reflection in background so it’s slow process. This method is created lot of
temporary objects that is maintain garbage collection.
Parcelable: Parcelable is much
faster than Serializable. Parcelable is take more time to implement compared to
Serializable interface. We can pass array via intent in Parcelable. It’s not
use reflection.
If u not want to send
any variable data to another class throe intent so you can go parcelable class and don’t write here that variable or
remove from here that variable.
Sync Activity and
Fragment life cycle?.
Activity: onCreate()
Fragment:
onAttach(), onCreate(),onCreateView(), onViewCreated(), onActivityCreated()
Activity: onStart()
Fragment:
onStart()
Activity: onResume()
Fragment:
onResume()
Activity: onPause()
Fragment:
onPause ()
Activity: onStop()
Fragment:
onStop
Activity: onDestroy()
Fragment:
onDestroyView(), onDestroy(), onDettach()
Types of design
pattern
v
Factory Pattern : When we have one
super class and multiple sub classes and we want to return one sub class then factory
design pattern is best options.
v
Adapter Design
Pattern:
In this pattern we are connecting two different component with adapter. In
other word the adapter pattern make two incompatible interface compatible
without changing existing property. Like Recycler view and Adapter
v
Abstract Factory
Pattern
v
Prototype Pattern
v
Singleton Pattern
v
Builder Pattern
Difference
between Service and IntentService ?
Service
class run on Main Thread while IntentService is running worker thread.
Service
class is base class of IntentService.
IntentService
is using queue process and handle on queue is onHandleIntent() this is stop
automatically when no intent in queue is empty while service class need to
manual stopSelf().
Service
class can triggered any thread while IntentService is trigger only from
MainThread.
If
you are using service there are chance to block main thread bcz service is
running main thread while IntentService is running separate thread on the basic
first come first serve. If you are using IntentService then you will find
difficult to interact with UI of the application.
Bound
Service is used when one or more than one application component bind the
service by using bindService() method.
If the application unbind the service then the service will be destroyed.
Looper and
Handler?
Android
Looper
is a wrapper to attach MessageQueue
and Thread and it manage queue
processing. In other word to say Looper is a class which is used to process
message in a queue. Normal thread have no such queue that means simple thread
does not contain any queue.
Lopper = Thread +
MessageQueue
Handler is a utility class
and this is allow to send and process message and runnable object associate with thread and MessageQueue.
Use:
Use:
- Message creation
- Inserting messages into the queue
- Processing message on the consumer thread
- Managing messages in the queue
class LooperThread extends Thread {
public Handler mHandler;
@Override
public void run() {
Looper.prepare();
mHandler = new Handler() {
@Override
public void
handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
How to send data
from service to activity?
Broadcast
Receiver that is transfer data from service to activity.
public class TrackWifiService extends IntentService {
public TrackWifiService() {
super("wifiService");
}
protected void onHandleIntent(@Nullable Intent intent) {
sendDataToActivity()
}
private void sendDataToActivity()
{
Intent sendLevel = new Intent();
sendLevel.setAction("GET_SIGNAL_STRENGTH");
sendBroadcast(sendLevel);
}
}
public class TrackWifiActivity extends AppCompatActivity {
WifiLevelReceiver receiver;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.track_wifi_activity_layout);
receiver = new WifiLevelReceiver();
registerReceiver(receiver, new IntentFilter("GET_SIGNAL_STRENGTH")); //<----Register
}
@Override
public void onStop()
{
super.onStop();
unregisterReceiver(receiver); //<-- Unregister to avoid memoryleak
}
class WifiLevelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("GET_SIGNAL_STRENGTH"))
{
int level = intent.getIntExtra("LEVEL_DATA",0);
// Show it in GraphView
}
}}}
MVVM
Observer design
pattern?
Observer design pattern is a
software design pattern that is established one-to-many dependency between
objects. The state of one object(Observable
or Subject) can change all other objects(Observer) state.
mMutableLiveData.observe(this, new Observer<String>() {
@Override
public void
onChanged(@Nullable String s) {
mTextView.setText(s);
}
});
Repository
1: Repository
is fetch data from web or local.
2:
This place is developer write own business logic.
ViewModel
ViewModel is a helper class which is
part of Android Architecture Component
it helps store, holds and prepare UI related data in lifecycle conscious way. ViewModel is update UI with the data change. The
main profit of ViewModel if Activity or Fragment destroyed by the system and
recreate this is use same ViewModel and same state. ViewModel maintain the own
lifecycle. View Model should not have any reference to Lifecycleowner like Activity or Fragment.
ViewModelFacory
1:
ViewModelFactory is create dynamic viewmodel for Activity and Fragment.
2:
ViewModelFactory is provide viemodel to Activity or fragment.
3:
ViewModelFactory is maintain the status of Activity or Fragment live or dead.
LiveData
LiveData is a observable data holder
class. Which is save/hold any object in that property. LiveData is follow
Observable Design Pattern. LiveData is aware life cycle.
MutableLiveData
MutableLiveData is a sub-class of
LiveData. Its provide setValue and postvalue method. If you are updating value
from MainThread then u can use setValue and if you want update value from
different thread then you must use postvalue method. MutableLiveData/MediatorLiveData
for notifying on each time of changing data.
mMutableLiveData.observe(this, new Observer<String>() {
@Override
public void
onChanged(@Nullable String s) {
mTextView.setText(s);
}
});
Dagger-2
Dagger2
Dagger is a dependency injection
which is simplify developer work and less code.
Component: Component annotation used for connection between
module and classes. We define the @Component.Builder interface which will be
called from our custom Application class.
Module: Module class are responsible for provide
object/class which can be injected.
Provider: Provider annotation is responsible for provide
object these method that is available in injection.
Singleton: Singleton annotation is responsible for create
single object any class and provide global level.
ViewModelKey: ViewModelKey is help for
map your ViewModel with ViewModelFactory.
No comments:
Post a Comment