logo
down
shadow

Does using the fragments lower down the load on main activity


Does using the fragments lower down the load on main activity

By : user3044946
Date : December 01 2020, 04:50 PM
Any of those help Fragments are really useful. If you think about it, they are really two activities working together as one cohesive "unit".
Advantages:
code :


Share : facebook icon twitter icon
Add fragments before main activity shows main content view

Add fragments before main activity shows main content view


By : MintyFresh314
Date : March 29 2020, 07:55 AM
hop of those help? You could try replacing your fragments in your FragmentActivity, here is the idea partially coded: Suppose you have your fragments layout like this (main.xml):
code :
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">

    <LinearLayout android:id="@+id/waiting" ...>
    </LinearLayout>

    <!-- hidden layout -->
    <LinearLayout>
        <LinearLayout android:id="@+id/layout_list_items" ...>
        </LinearLayout>
        <LinearLayout android:id="@+id/layout_detail" ...>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
public class FragmentsActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);

        Fragment fragA = new WaitingTransaction();

        FragmentTransaction fragTrans = this.getSupportFragmentManager().beginTransaction();
        fragTrans.add(R.main.waiting, fragA);
        fragTrans.commit();
    }

    private void afterProcessing(){

                //show hidden layout and make the waiting hidden through visibility, then add the fragment bellow...
        FragmentTransaction fragTrans = this.getSupportFragmentManager().beginTransaction();
        fragTrans.add(R.main.layout_list_items,
                          new FragmentList());
        fragTrans.replace(R.main.layout_detail,
                          new FragmentB());
        fragTrans.commit();
    }

}
Load custom fragments in Navigation-Drawer's drawer and open corresponding fragments in the main layout

Load custom fragments in Navigation-Drawer's drawer and open corresponding fragments in the main layout


By : Rumeysa Nur Ozbay
Date : March 29 2020, 07:55 AM
may help you . You should create interfaces for your fragments and then use callback methods to alert the activity. Here is a sample code for you.
code :
 public class FirstDrawerFragment extends Fragment {

    FirstDrawerListener mListener;

    public interface FirstDrawerListener{
        public void onSomeThingHappend(Data youWantToPass);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mListener = (FirstDrawerListener)activity;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragnavfirst, container , false);

        v.findViewById(R.id.someview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onSomeThingHappend(data);
            }
        });
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

public class SecondDrawerFragment extends Fragment {

    SecondDrawerListener mListener;

    public interface SecondDrawerListener{
        public void onSomeThingHappendSecond(Data youWantToPass);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mListener = (SecondDrawerListener)activity;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragnavfirst, container , false);

        v.findViewById(R.id.someview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onSomeThingHappend(data);
            }
        });
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}


public class YourActivity extends Activity FirstDrawerListener ,SecondDrawerListner{

    @Override
    public void onSomeThingHappend(Data youWantToPass){

        //do your thing and add some fragments
        //close drawer
    }

    @Override
    public void onSomeThingHappendSecond(Data youWantToPass){

        //do yout thing and add some fragments
        //close drawer
    }

}
pass string from fragment main activity to fragments activity in viewpager

pass string from fragment main activity to fragments activity in viewpager


By : Nico De Niro
Date : March 29 2020, 07:55 AM
I hope this helps you . You don't have access directly to your fragments that reside in ViewPager so you can't reference them directly.
What I am doing in these cases is send a broadcast message from Activity to Fragments. For this reason register a BroadcatReceiver in the fragment (either in onCreate or onCreateView - your decision)m, set a custom action for that receiver (ex. "my_package.actions.internal.BROADCAST_ACTION"), don't forget to unregister the receiver from complementary method.
code :
public class Store_Swipe extends Fragment {

    public static final String ACTION_INTENT = "my_package.action.UI_UPDATE";

    protected BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(ACTION_INTENT.equals(intent.getAction())) {
                String value = intent.getStringExtra("UI_KEY");
                updateUIOnReceiverValue(value);
            }
        }
    };

    private void updateUIOnReceiverValue(String value) {
        // you probably want this:
        kata_keyword.setText(value);
    }

    String KeyWord;
    private TextView kata_keyword;

    public static Fragment newInstance(Context context) {
        Store_Swipe f = new Store_Swipe();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter(ACTION_INTENT);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
    }

    @Override
    public void onDestroy() {
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
        super.onDestroy();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();
        KeyWord = bundle.getString("keyword");
        View view = inflater.inflate(R.layout.store_swipe, container, false);
        init(view);

        return view;

    }

    void init(View view) {
        kata_keyword = (TextView) view.findViewById(R.id.keyword);
        ImageView image = (ImageView) view.findViewById(R.id.image_error);
        kata_keyword.setText(KeyWord);
    }
}
protected void sendValueToFragments(String value) {
    // it has to be the same name as in the fragment
    Intent intent = new Intent("my_package.action.UI_UPDATE");
    intent.putExtra("UI_KEY", value);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
findViewById(R.id.button_id).setOnClickListener(new OnClickListener() {            
    @Override
    public void onClick(View v) {
        String valueThatYouWantToSend = null; /// just the value
        sendValueToFragments(valueThatYouWantToSend);       
    }
});
i have a floating button in main activity.i want to clear all fragments and return to Main Activity

i have a floating button in main activity.i want to clear all fragments and return to Main Activity


By : sree sanjeev
Date : March 29 2020, 07:55 AM
I hope this helps . Trick is simply you start main activity on FloatingActionButton onClick event
code :
        bt_home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivity(new Intent(getActivity(), MAinActivity.class));
             //or start home fragment
         Fragment home = new HomeFragment();
         FragmentManager FM = getFragmentManager();
         FM.beginTransaction().replace(R.id.content_frame, home).commit();

        }
    });
How to make the fragments load data from a service even before they are being displayed in the main activity?

How to make the fragments load data from a service even before they are being displayed in the main activity?


By : Fear Cindy
Date : March 29 2020, 07:55 AM
To fix this issue I solved it following this guide: https://medium.com/@oluwabukunmi.aluko/bottom-navigation-view-with-fragments-a074bfd08711
Instead of replace(), it says I have to use show() and hide(). I only had to modify my MainActivity:
code :
final ColorsFragment colorsFragment = new ColorsFragment();
final LyricsFragment lyricsFragment = new LyricsFragment();
final LastFmFragment lastFmFragment = new LastFmFragment();

final FragmentManager fragmentManager = getSupportFragmentManager();
Fragment activeFragment = colorsFragment;

BottomNavigationView bottomNavigationView;

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

    if (!isNotificationServiceRunning()) {
        startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
    }

    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(menuItem -> {

        switch (menuItem.getItemId()) {
            case R.id.nav_colors:
                fragmentManager.beginTransaction().hide(activeFragment).show(colorsFragment).commit();
                activeFragment = colorsFragment;
                break;
            case R.id.nav_lyrics:
                fragmentManager.beginTransaction().hide(activeFragment).show(lyricsFragment).commit();
                activeFragment = lyricsFragment;
                break;
            case R.id.nav_last_fm:
                fragmentManager.beginTransaction().hide(activeFragment).show(lastFmFragment).commit();
                activeFragment = lastFmFragment;
                break;
        }

        return true;
    });

    fragmentManager.beginTransaction().add(R.id.fragment_container, lastFmFragment, "2").hide(lastFmFragment).commit();
    fragmentManager.beginTransaction().add(R.id.fragment_container, lyricsFragment, "1").hide(lyricsFragment).commit();
    fragmentManager.beginTransaction().add(R.id.fragment_container, colorsFragment, "0").commit();
}
Related Posts Related Posts :
  • Android Broadcastreceiver for other apps install/delete not working
  • Android Studio onClick not working in BindViewHolder
  • How to use Spring Converter for some controllers only?
  • How verify that 3 numbers in sequence are equals?
  • When using .compareTo to compare dates, why doesn't it take Months into account?
  • Does the perfomance of "filter then map" and "map then filter" differ in a Stream?
  • How can I set the initial Delay after pressing the start Button to a specific time (HH:mm:ss) format
  • How to switch between Android devices during the tests
  • How to configure java.util.logging via properties to use standard output?
  • How to iterate through array in order
  • Is there better way of iteration to find the evenly divisible number?
  • How to split a string to non empty words if it might include a separator like tab on first place
  • Supplier<Sequence<String>> cannot be iterated more than once
  • Why there is only one thread can actually started in @PostConstruct method?
  • Manage CompletionStage inside of Netty handler
  • Url Problem while Developing on Localhost and deploy on Remote Virtual Server
  • How to identify the missing type id in Jackson error?
  • android data binding error: cannot find symbol
  • Spring Boot application with a jar dependency does not run after maven build
  • Spring Data JPA query , filter ? search engine ? JPQL?
  • Why LiveData returns null in ViewModel?
  • what this line of code mean....new URLClassLoader(new URL[0],getClass().getClassLoader());
  • Why do need to use new Random() instead of just Random Randomnum?
  • I want to access zk components from the java file
  • How do I cast FieldValue.serverTimestamp() to Kotlin/Java Date Class
  • Insertion Sort Double Array with User Input - JAVA
  • Creating 2 dimesional array with user input and find sum of specific columns
  • can not get Advertising ID Provider in android
  • Convert list of Objects to map of properties
  • How to represent an undirected weighted graph in java
  • Return values as array from collection
  • ByteBuddy generic method return cast to concrete type
  • ImageView hides the round corners of the parent
  • Is there a way to find setter method by its getter method or vice versa in a class?
  • Get aggregated list of properties from list of Objects(Java 8)
  • Unable to find a document in Mongodb where exact date match in java
  • UsernamePasswordAuthenticationFilter skips success handler
  • Use Java filter on stream with in a stream filter
  • Default Login not successful in spring boot 2.1.7
  • Adding key value pairs from a file to a Hashmap
  • Rub regex: matching a char except when after by another char
  • Convert Base64 String to String Array
  • Escape Unicode Character 'POPCORN' to HTML Entity
  • An empty JSON field which is a boolean/nullable field in Java model, is getting converted as null
  • Mongo java driver cannot find public constructor for interface
  • How to unit test writing a file to AWS Lambda output stream?
  • How to make a GitHub GraphQL API Call from Java
  • What's the difference between @ComponentScan and @Bean in a context configuration?
  • Expected class or package adding a view using a class
  • can be delete of a element in a static array be O(1)?
  • Instance variable heap or stack ? ( with specific example)
  • Assert progress of ProgressBar in Espresso test
  • How to detect if gson.fromjson() has excess elements
  • I cant generate the proper code to select the a specific filter on a BI dashboard I am working on
  • How to Inject Dependencies into a Servlet Filter with Spring Boot Filter Registration Bean?
  • Thrift types as a Generic
  • Effective algorithm to random 4 unique integers less than a big max such as 100_000
  • Combining or and negation in Java regex?
  • Unable to instantiate default tuplizer Exception
  • Multi-tenant migration to work with quarkus
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org